jpa

Again with JPA not making sense

I asked this earlier and was to look at mapped by. I have 2 tables: A s_id(key) name cli type B sa_id(key) s_id user pwd. So in Jpa I have: @Entity class A...{ @OneToMany(fetch=FetchType.EAGER) @JoinTable( name="A_B", joinColumns={@JoinColumn(name="a_id", table="a",unique=false)}, inverseJoinColumns={@JoinCo...

JPA and inheritance

I have a some JPA entities that inherit from one another and uses discriminator to determine what class to be created (untested as of yet). @Entity(name="switches") @DiscriminatorColumn(name="type") @DiscriminatorValue(value="500") public class DmsSwitch extends Switch implements Serializable {} @MappedSuperclass public abstract class ...

Fix for Hibernate error "Use of the same entity name twice"

How you fix the following Hibernate error: What does "Use of the same entity name twice". ...

What is a good design for a query "layer" for Java JPA

In JPA the Entities are nice annotated Plain Old Java Objects. But I have not found a good way to interact with them and the database. In my current app, my basic design is always to have a sequence based id as primary key so I usually have to look up entities by other properties than PK. And for each Entity I have a stateless EJB of ...

Can I specify a JPA compliant persistence.xml file on the java command line?

I've got an UberJar with all my java classes, but want to execute this jar with an external persistence.xml file. Per the specification, Hibernate (or any other JPA provider) looks for the persistence.xml file in any META-INF folder that's on the classpath, but I haven't been able to make this work with an UberJar. Any ideas? Is the U...

Where (which layer) to put Entity query methods, "persist" methods etc. ?

Hi, I have a SEAM app with some JPA/Hibernate entities. And I now wonder where to put my query, persistence methods. The default choice seems to put them in a session bean layer with injected @PersistenceContext(...) @Inject EntityManager entityManager; But I think I would rather have the methods on the entities themselves. What ar...

What is the best practice for JPA/Hibernate entity classes and synchronization?

It seems like most examples of JPA/Hibernate entity bean classes I've seen do no explicit synchronization. Yet, it is possible to call getters/setters on those objects in the context of building up a transaction. And it's possible for those methods to be called across multiple threads (although maybe that's unusual and weird). It se...

JPA - many to many relationship of a table with a compound-key with itself

I would like to create a many to many relationship of a table (RoutePlace) with itself using JPA anotations. The singularity of the table I want to join with itself is that it has a compound-key. example: http://h4losw2.files.wordpress.com/2008/10/tables.png Any suggestions? Thanks in advance ...

Setting default values for columns in JPA

Is it possible to set a default value for columns in JPA, and if, how is it done using annotations? ...

Exception when not finding

What is the rationale behind throwing an exeption in JPA, when something can't be found? Added: This behaviour was seen in EJB2, and has as far been removed from EJB3 - but... It remains when calling Query#getSingleResult() which throws a NoResultException. Normally I would not expect it to be an exception that something could not be fo...

Which anotation should I use: @IdClass or @EmbeddedId

The JPA (Java Persistence API) specification has 2 different ways to specify entity composite keys: @IdClass and @EmbeddedId. I'm using both annotations on my mapped entities, but it turns out to be a big mess to people who aren't very familiar with JPA. I want to adopt only one way to specify composite keys. Which one is really the ...

Customized naming of columns in JPA relationships

When I make relationsships with JPA using hibernate, some terrible long and ackward column names are generated. Eg. I have the following actionPlan_actionPlanPK, which means that the column actionPlan is a FK that points to actionPlanPK. To make it look just a little bit more neat in the DB I would like if I could give it a name myself, ...

Do I have to close() every EntityManager?

I have just started migrating my homegrown persistence framework to JPA. Given that the persistence frameworks hide a lot of the plumbing, I'm interested in knowing if NOT closing EntityManagers will create a resource leak, or if the frameworks will collect and close them for me. I intend in all places to close them, but do I HAVE to? ...

Hibernate returns invalid results with composite key.

I'm getting the strangest results. I have a class with a composite key. If i do the following query: from LOVEJB l order by l.canonicalId desc my results are not ordered by the 'canonicalId' column I asked for. Instead, the canonical id result is like: 823 823 822 823 ,,, Can someone give me some pointers on how should I tr...

JPA Composite Key + Sequence

Hi, Is it possible in plain JPA or JPA+Hibernate extensions to declare a composite key, where an element of the composite key is a sequence? This is my composite class: @Embeddable public class IntegrationEJBPk implements Serializable { //... @ManyToOne(cascade = {}, fetch = FetchType.EAGER) @JoinColumn(name = "APPLICAT...

How JPA (Hibernate) deal with transaction when fetching Object from database

Hi all, I'm currently developping an application in java using Hibernate as a persistence manager and JPA as an abstraction of the persistence manage hibernate. I'd like to know the impact of wrapping a result query around a transaction. I know the entity manager must stay open for lazily fetched field bug what about transaction in al...

EJB3 Business Logic Patterns & Practices

I'm in the process of developing a multi-tiered financial processing application in Java using EJB3 (Hibernate + Glassfish for the app and web services layer, Lift on Glassfish for the web UI) and I'm struggling with the question of where to put my business logic. When this project started, our first notion was to put the bulk of our bu...

JPA / Hibernate Select Column Subset on Join

In SQL it is easy to do a join and only return the columns you want from the joined table. What is the best way to map this in JPA / Hibernate? For example, there is a Folder entity mapped to the EMAIL_FOLDER and an Email entity mapped to the EMAIL table. There is a one-to-many relationship from Folder to Email. The Email entity is rath...

Spring & Hibernate EJB Events

Is it possible to define a spring-managed EJB3 hibernate listener? I have this definition in my persistence.xml: <properties> <property name="hibernate.ejb.interceptor" value="my.class.HibernateAuditInterceptor" /> <property name="hibernate.ejb.event.post-update" value="my.class.HibernateAuditTrailEventListene...

ORM solutions (JPA; Hibernate) vs. JDBC

I need to be able to insert/update objects at a consistent rate of at least 8000 objects every 5 seconds in an in-memory HSQL database. I have done some comparison performance testing between Spring/Hibernate/JPA and pure JDBC. I have found a significant difference in performance using HSQL.. With Spring/Hib/JPA, I can insert 3000-400...