-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Annotations no "Assigned" generator type
PostPosted: Fri May 18, 2007 3:30 pm 
Newbie

Joined: Fri May 18, 2007 2:21 pm
Posts: 3
A question that has been bugging me. Here is an example...

I have two tables (Mysql v5.1):

TABLE1: client
FIELD: case_id; int(11), Primary Key, Not Null
FIELD: client_name; varchar(255)

TABLE2: clientlog
FIELD: clientlog_id; int(11), Primary Key, Not Null, Auto Increment
FIELD: case_id; int(11)
KEY `client_index` (`case_id`), CONSTRAINT `clientlog_ibfk_1` FOREIGN KEY (`case_id`) REFERENCES `client` (`case_id`) ON DELETE CASCADE

So a client can have more than one log entry (oneToMany). I am using hibernate annotations to map all this in a very simple example.

It seems that Hibernate Annotations does not have an equivalent to generator class "assigned". The field "case_id" is not generated it is given. Instead I used:

Code:
@GeneratedValue(strategy=GenerationType.AUTO)

So if I try to do something like:

Code:
Client c = new Client();
c.setCaseid(24);
em.persist(c);


I get exceptions regarding Client being detached. Which makes sense as I am short-circuiting hibernate.

Does this mean I have to create a new primary key in the Client table just for hibernate? Or is there something else I could do? Interestingly if I use XML mapping (no annotations, using sessions) which gives me the generator class "assigned" it works just fine. It seems to be a limitation of hibernate annotations. Of course, it may have been coded that way for a reason.

Any thoughts on how to approach this would be great. At this point changing the database schema, or the code is a possibility. Just want to make sure I choose the best path.

Thank you.

_________________
A grain of sand is no more important in its relation to the sea, after all it is only a grain of sand.


Top
 Profile  
 
 Post subject: Sory to bump this but I am just really curious
PostPosted: Fri May 25, 2007 11:05 am 
Newbie

Joined: Fri May 18, 2007 2:21 pm
Posts: 3
What happens if the IDs are generated outside of the application. For example - a third party application. In this case the IDs are assigned not generated.

When using annotations it seems you can not use a generator type of assigned. But if you use hibernate XML files and sessions then it works. Maybe I am missing something (would not be the first time). But any information would be great.

Thank you.

_________________
A grain of sand is no more important in its relation to the sea, after all it is only a grain of sand.


Top
 Profile  
 
 Post subject: Hibernate Annotations no "Assigned" generator type
PostPosted: Mon May 28, 2007 7:00 am 
Newbie

Joined: Fri May 18, 2007 2:21 pm
Posts: 3
I figured it out. Do not specify a generator type, simply say it is an @Id.

@Id
private Long t_id;

Then you can use assigned ids.

_________________
A grain of sand is no more important in its relation to the sea, after all it is only a grain of sand.


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

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.