-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to use ManyToOne to look up by other column first?
PostPosted: Sun Oct 17, 2010 5:57 pm 
Newbie

Joined: Fri Apr 04, 2008 5:05 pm
Posts: 3
Hi,

I'm wondering if it's possible to use a ManyToOne annotation to 'find or create' on the reference table.

Code:
class Foo {
   @ManyToOne(fetch=FetchType.EAGER)
   @JoinColumn(name = "reference_table_id")
   ReferenceTable referenceTable;
...
}

class ReferenceTable {
   @Id
   @GeneratedValue
   @Column(name = "id")
   Integer id;

   @Column(name = "description")
   String description;
...
}


ReferenceTable can be null. A Foo object is created without any problems, but when I go to add a ReferenceTable to it later, it always creates a new entry. I'd like to look up by 'description' first and use that if it exists, otherwise create a new one.

I tried implementing this in code, but I get a TransientObjectException when I try to do the find. I think it smells that I have to do the find manually, so I probably don't understand how best to use the many-to-one relationship in hibernate.

Any help would be greatly appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.