There are 2 tables and 2 corresponding entities:
item - ItemEntity
type - TypeEntity
I have this relationship: type_id is foreign key that represent type table
This class must be embedded Id: (I have more complicated ones, this is only the simplest, with one Long id)
Code:
@Embeddable
public class ClassicId implements Serializable
{
@Column(name="id")
private Long id;
//hashCode and equals are overriden
//getters and setters
TypeEntityCode:
@Entity
@Table(name="type")
public class TypeEntity implements ITypeEntity, Serializable
{
@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY)
private ClassicId id;
@Column(name="code")
private String entityType;
//getters and setters
ItemEntityCode:
@Entity
@Table(name="item")
public class ItemEntity implements IItemEntity, Serializable
{
@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY) //[color=#FF0000] <-- THIS IS NOT WORKING FOR EMBEDDED ID[/color]
private ClassicId id;
@OneToOne
@JoinColumn(name="type_id")
private TypeEntity entityType;
Application test codeCode:
TypeEntity typeEntity = new TypeEntity();
ItemEntity itemEntity = new ItemEntity();
typeEntity.setEntityType("TEST_ENTITY");
itemEntity.setEntityType(typeEntity);
utx.begin();
typeDao.save(typeEntity);
itemDao.save(itemEntity);
utx.commit();
Error when I try to persistQuote:
INFO: Hibernate: insert into type (code, id) values (?, ?)
INFO: Hibernate: insert into item (type_id, id) values (?, ?)
WARNING: SQL Error: 1048, SQLState: 23000
SEVERE: Column 'type_id' cannot be null
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Now, If I replace
@EmbeddedID classicID class with normal
@ID and Long id is working perfectly. But with embeddedID it cannot update the ID in type_id.
BTW: all annotation are from javax.persistence.*
What can I do?