-->
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.  [ 2 posts ] 
Author Message
 Post subject: Correct definition of a column that is both a PK and an FK
PostPosted: Thu Jan 24, 2008 11:24 am 
Newbie

Joined: Fri Jun 15, 2007 2:32 pm
Posts: 16
Hello,

I have the following scenario:

Schema:

CAR
---------------
CAR_ID [PK]
NAME

CAR_ATTR
---------------
CAR_ID [PK, FK]
NAME [PK]
VALUE

Auto generated classes:

public class Car {
private Integer carId;
private String name;
...
}

public class CarAttr {
private Integer carId;
private String name;
private String value;
private Car car;
...
}

public class CarAttrPK {
private Integer carId;
private String name;
...
}



When trying to insert a new CarAttr I get the following exception.
I guess that the exception is caused because the classes are not annotated correctly and Hibernate tries to insert the carId twice.

What is the correct way to represent the schema?

Thank you.

Hibernate version:
3.3.1

Full stack trace of any exception that occurs:

17:16:20,359 INFO StringType:144 - could not bind value 'fdas' to parameter: 4; Parameter index out of range (4 > number of parameters, which is 3).
17:16:20,359 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: S1009
17:16:20,359ERROR JDBCExceptionReporter:78 - Parameter index out of range (4 > number of parameters, which is 3).
17:16:20,359ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session

Name and version of the database you are using:
MySql 5

The generated SQL (show_sql=true):
/* insert mycomp.CarAttr
*/ insert
into
mydb.car_attr
(VALUE, CAR_ID, NAME)
values
(?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 5:04 am 
Newbie

Joined: Fri Jun 15, 2007 2:32 pm
Posts: 16
Ohad Assulin from http://www.metalogic.co.il advised me to use EmbeddedId that allows to declare a primary key that is an entity and not basic.

public class Car {
private Integer carId;
private String name;
...
}

public class CarAttr {

@EmbeddedId
private CarAttrPK carAttrPK;

private String name;

private String value;
...
}

@Embeddable
public class CarAttrPK {

@ManyToOne
@JoinColumn(name = "CAR_ID", nullable = false)
private Car car;

private String name;
...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.