Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Should I always use ORM in entities.. or can I use integers and queries?  RSS feed

john lazeraski
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I am having some odd ORM issues.. can't really figure it out yet. But it leads me to ask if ORM is always the way to go with entity beans, or if sometimes there is nothing wrong with just storing integer IDs and using my own named queries to pull data when I need it. I love what JPA provides, but at the same time, it was easier to me using JDBC directly.. it might have been more code, but at least I knew I had to make calls to get data and joins and such. I think my issue is relationships. Individual entities are no problem. As soon as I have a OneToMany, and insert on of the childs, things go nuts. Sometimes, it works, other times, I seem to get a continuous loop of inserts.. and then my container crashes.

I have something like this:

The problem seems to happen when I go to insert a contact.. in our system users register and have 0 contacts.. but at some later point will manually type in a contact. I'll go to insert that contact. I do so much like when I insert a new user..something like:


Now.. that works, but the relationship to the user is not set up. I actually have a method where I pass in a Contact object, which is a pure pojo with just the fields, no annotations, not an entity..something like:

So in the above example, I try to insert a new contact, and find the UserEntity owner, and set it on the new contact entity instance, then persist. When I do this, however, my glassfish 3.1 basically stops working.. I have a bunch of debug output that seems to indicate that its looking up the user.. and repeatedly does this quite a few times.

So in the above, am I doing something wrong? Should I simply get the user and ADD the contact to the collection of contacts and that will persist the new contact too? I always thought I'd have to persist the individual entity before I can add it to a collection of a one to many relationship?

See.. in this case, just using long or integer id fields, I could persist the contact by itself, then look up the user, add the refreshed contact id to the user, and merge that. Then have a query that can look up the contacts for the user id. It would be simpler I think.. although I don't know if it would be more performant, less memory use, etc than using ORM. But I still can't help wondering if I always should be setting up these mappings with ORM or if I should keep things as entities and handle the queries myself.


Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!