-->
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: hibernate keeps creating id column in db for embedded key
PostPosted: Sun May 29, 2011 4:40 pm 
Beginner
Beginner

Joined: Thu Jan 12, 2006 6:32 pm
Posts: 39
Location: Austin, Tx, USA, NA, Sol 3
I'm having a problem with hibernate creating an integer id column for what is actually an embedded id. The Hibernate libs in use are:

<version.hibernate>3.3.1.GA</version.hibernate>
<version.hibernate-commons-annotations>3.3.0.ga</version.hibernate-commons-annotations>
<version.hibernate-annotations>3.4.0.GA</version.hibernate-annotations>
<version.hibernate-validator>3.1.0.GA</version.hibernate-validator>
<version.hibernate-entitymanager>3.4.0.GA</version.hibernate-entitymanager>
<version.hibernate-entityversioning>1.2.2.GA-hibernate-3.3</version.hibernate-entityversioning>
<version.hibernate-search>3.1.0.GA</version.hibernate-search>

There is an extensive class hierarchy in play which may be part of the problem, but I cannot figure out how.

Code:
@MappedSuperclass
public abstract class Domain<ID extends Serializable> {

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="id",insertable=true,nullable=false,unique=true,updatable=false)
   private ID id;

@MappedSuperclass
public abstract class BaseEntity<ID extends Serializable> extends Domain<ID> {
// contains fields for create/update who/when, and version   

@MappedSuperclass
public abstract class InfoTableEntity<ID extends Serializable> extends BaseEntity<ID> implements Serializable {
// contains a single int field "inactive" which is used as a boolean

@Audited
@Entity
@Table(name="sys_user")
public class SysUser extends InfoTableEntity<Integer> implements Serializable {
...
   @Audited(targetAuditMode=RelationTargetAuditMode.AUDITED)
   @Fetch(FetchMode.SELECT)
   @IndexColumn(name="id",nullable=false)
   @OneToMany(fetch=FetchType.EAGER,mappedBy="sysUser")
   private List<XrefUserGroup> sysGroupMembershipCollection;


All of the above works as expected. The User and Group tables are correct. However, the join table between two is not.

I have:

Code:
@Embeddable
public class XrefUserGroupId implements Serializable {

   @Column(name="sys_group_id", unique=true, nullable=false, insertable=true, updatable=false)
   private int sysGroupId;

   @Column(name="sys_user_id", unique=true, nullable=false, insertable=true, updatable=false)
   private int sysUserId;

@MappedSuperclass
public abstract class BaseEntityCK implements Serializable {
// save as the one above, but without the <ID extends Serializable> extends Domain<ID>
   
@Audited
@Entity
@Table(name="xref_user_group")
public class XrefUserGroup extends BaseEntityCK implements Serializable {

   @EmbeddedId
   private XrefUserGroupId id;

   //bi-directional many-to-one association to SysUser
   @Audited(targetAuditMode=RelationTargetAuditMode.AUDITED)
   @ForeignKey(name="fk_xug_su_userid")
   @JoinColumn(name="sys_user_id", nullable=false, insertable=false, updatable=false)
   @ManyToOne(fetch=FetchType.EAGER,optional=false)
   private SysUser sysUser;


and what I end up with is:

Code:
CREATE TABLE xref_user_group (
    sys_group_id INT NOT NULL,
    sys_user_id INT NOT NULL,
    cr_when DATE NOT NULL,
    cr_who VARCHAR(20) NOT NULL,
    master_key VARCHAR(60),
    record_version INT NOT NULL,
    up_when DATE NOT NULL,
    up_who VARCHAR(20) NOT NULL,
    effective_date DATE,
    inactive INT NOT NULL,
    id INT,
    PRIMARY KEY (sys_group_id, sys_user_id),
    CONSTRAINT fk_xug_gu_groupid FOREIGN KEY (sys_group_id) REFERENCES sys_group (id) ,
    CONSTRAINT fk_xug_su_userid FOREIGN KEY (sys_user_id) REFERENCES sys_user (id),
    CONSTRAINT sys_group_id UNIQUE (sys_group_id),
    CONSTRAINT sys_user_id UNIQUE (sys_user_id),
    INDEX fk_xug_gu_groupid (sys_group_id),
    INDEX fk_xug_su_userid (sys_user_id)
)


the "id INT" column is not necessary and the constraints:

Code:
    CONSTRAINT sys_group_id UNIQUE (sys_group_id),
    CONSTRAINT sys_user_id UNIQUE (sys_user_id),


are very wrong.

Can any one point me in a direction? Thanks!


Top
 Profile  
 
 Post subject: Re: hibernate keeps creating id column in db for embedded key
PostPosted: Sun May 29, 2011 5:18 pm 
Beginner
Beginner

Joined: Thu Jan 12, 2006 6:32 pm
Posts: 39
Location: Austin, Tx, USA, NA, Sol 3
BAH Found it.

An @IndexColumn annotation was referencing "id" instead of "user_id".

That should generate a warning or something.


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.