-->
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.  [ 6 posts ] 
Author Message
 Post subject: how to use @IdClass
PostPosted: Mon Feb 05, 2007 10:17 am 
Newbie

Joined: Mon Feb 05, 2007 9:47 am
Posts: 4
Location: Tokyo
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.1 GA with JDK 1.6
Name and version of the database you are using: Oracle9iR2

When I use @IdClass for composite primary key, I get the following error.
Do I misunderstand how to use @IdClass?
Or is this a known issue?

Any help would be appreciated.
Thanks in advance,

ebi

Code:
javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing column: id1 in TEST.TEST
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
   at mypackage.TestDriver.main(TestDriver.java:14)
Caused by: org.hibernate.HibernateException: Missing column: id1 in TEST.TEST
   at org.hibernate.mapping.Table.validateColumns(Table.java:254)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1080)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
   ... 4 more
org.hibernate.HibernateException: Missing column: id1 in TEST.TEST
   at org.hibernate.mapping.Table.validateColumns(Table.java:254)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1080)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
   at mypackage.TestDriver.main(TestDriver.java:14)


Code:
// DDL
CREATE TABLE TEST (
  ID_1 INTEGER NOT NULL,
  ID_2 INTEGER NOT NULL,
  CONSTRAINT TEST_PK PRIMARY KEY (ID_1, ID_2)
);

// Test.java
@Entity
@IdClass(TestPK.class)
public class Test implements Serializable {
    private Integer id1;
    private Integer id2;

    @Id
    @Column(name="ID_1", nullable = false)
    public Integer getId1() {
        return id1;
    }

    public void setId1(Integer id1) {
        this.id1 = id1;
    }

    @Id
    @Column(name="ID_2", nullable = false)
    public Integer getId2() {
        return id2;
    }

    public void setId2(Integer id2) {
        this.id2 = id2;
    }
}

// TestPK.java
public class TestPK implements Serializable {
    private Integer id1;
    private Integer id2;

    public Integer getId1() {
      return id1;
   }

   public void setId1(Integer id1) {
      this.id1 = id1;
   }

   public Integer getId2() {
      return id2;
   }

   public void setId2(Integer id2) {
      this.id2 = id2;
   }
}

// Begin test
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();

em.getTransaction().begin();

Test test = new Test();
test.setId1(1);
test.setId2(1);
em.persist(test);

em.getTransaction().commit();

em.close();
emf.close();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this is a small bug in HAN, you need to define @Column(name=id_1) on test.id1 too

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 10:05 pm 
Newbie

Joined: Mon Feb 05, 2007 9:47 am
Posts: 4
Location: Tokyo
Thank you, Emmanuel.

emmanuel wrote:
this is a small bug in HAN, you need to define @Column(name=id_1) on test.id1 too


Could you point the bug number?

I tried adding @Column to each property but I'm still meeting the same error...

Code:
@Entity
@IdClass(TestPK.class)
public class Test implements Serializable {
    @Column(name="ID_1", nullable = false)
    private Integer id1;

    @Column(name="ID_2", nullable = false)
    private Integer id2;

    @Id
    @Column(name="ID_1", nullable = false)
    public Integer getId1() {
        return id1;
    }

    public void setId1(Integer id1) {
        this.id1 = id1;
    }

    @Id
    @Column(name="ID_2", nullable = false)
    public Integer getId2() {
        return id2;
    }

    public void setId2(Integer id2) {
        this.id2 = id2;
    }
}


If I comment out the @IdClass line and the @Id line for ID_2, it does work.

Thanks in advance,

ebi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 10:27 pm 
Newbie

Joined: Mon Feb 05, 2007 9:47 am
Posts: 4
Location: Tokyo
I could found the bug entry myself.
http://opensource.atlassian.com/project ... se/ANN-361

But I've not understand how to work around this yet...
Any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 11:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
// TestPK.java
public class TestPK implements Serializable {
    private Integer id1;
    private Integer id2;

    @Column(name="id_1") public Integer getId1() {
      return id1;
   } ...

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:17 am 
Newbie

Joined: Mon Feb 05, 2007 9:47 am
Posts: 4
Location: Tokyo
Thanks a lot!
I could finally execute without errors.


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