-->
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: Embedded ID problem
PostPosted: Sun Oct 03, 2010 6:04 pm 
Newbie

Joined: Sun Oct 03, 2010 5:44 pm
Posts: 2
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

Image

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


TypeEntity
Code:
@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


ItemEntity
Code:
@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 code
Code:
           
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 persist
Quote:
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?


Top
 Profile  
 
 Post subject: Re: Embedded ID problem
PostPosted: Mon Oct 04, 2010 4:03 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
remove ClassicId, there are no advantages using it

for cases where embedded id represents complex primary keys, assign all id values manually


Top
 Profile  
 
 Post subject: Re: Embedded ID problem
PostPosted: Mon Oct 04, 2010 5:53 am 
Newbie

Joined: Sun Oct 03, 2010 5:44 pm
Posts: 2
maint175 wrote:
remove ClassicId, there are no advantages using it

for cases where embedded id represents complex primary keys, assign all id values manually


I know is a silly example with ClassicId, but I really have some complex ones on the way. I wanted to make a proof of concept with a simple model (in this case, ClassicId).

I thought to assign values manually. Should I use a table for that? Or there is another method?

Sorry if my question sounds silly but I am kind of new in persistence. The books I have are not so clear about many issues. You have to read 4 books, google-it to complete the missing info ...etc. But I am willing to learn :)


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.