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.