-->
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: Mapping Join Table with additional Columns & composed PK
PostPosted: Tue Sep 09, 2008 11:47 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
Hi,
I have a many to many relation with a join table that have additional columns and a composed PK.

Why do I get a
Code:
org.hibernate.HibernateException: Missing column: developer_DEVELOPER_ID in ESVPROTO.PR3_DEVELOPER_ASSIGNMENT
Exception ?

The column name and referencedColumName are set.

The Classes looks as follows:

Developer Class:


Code:
@Entity
@Table( name = "PR3_DEVELOPER" )
public class Developer extends BaseVersionedEntity
{
....
  @OneToMany( cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "developer" )
  private Set<DeveloperAssignment> developerAssignment = new HashSet<DeveloperAssignment>();



DeveloperAssignment Class (join class):

Code:
@Entity
@Table( name = "PR3_DEVELOPER_ASSIGNMENT" )
public class DeveloperAssignment extends BaseRelationEntity<StoryAssignment, Developer, DeveloperAssignmentPK>
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  private DeveloperAssignmentPK primaryKey;

  @SuppressWarnings( "unused" )
  @ManyToOne( optional = false )
  @PrimaryKeyJoinColumn( name = "STORY_ASSIGNMENT_ID", referencedColumnName = "STORY_ASSIGNMENT_ID" )
  private StoryAssignment storyAssignment;

  @SuppressWarnings( "unused" )
  @ManyToOne( optional = false )
  @PrimaryKeyJoinColumn( name = "DEVELOPER_ID", referencedColumnName = "DEVELOPER_ID" )
  private Developer developer;


The DeveloperAssignmentPK class:
Code:
@Embeddable
public class DeveloperAssignmentPK extends BasePrimaryKey
{

  @Column( name = "STORY_ASSIGNMENT_ID" )
  private Integer storyAssignmentId;

  @Column( name = "DEVELOPER_ID" )
  private Integer developerId;
...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 7:57 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
the embeddable PK is missing some required attributes,
you need to set @Id on each field and appropriate attribute overrides;
see the examples in the reference.

please read them carefully, and don't forget to override equals() and hashCode() appropriatly.
Having to map a dual PK is a bit tricky, I would suggest to use the Hibernate Tools to see how it is mapping your tables.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 3:45 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
Thanks for your post.
As I am using an @EmmbededId , I don't need to specify the @Id annotation on the attributes.
The Fields of the PK Class must at least be protected. I changed it. The equals and HashCode method were defined in the PK Super Class.
And now I also used the @AttributeOverrideattribute.

I am still getting the same error metioned above.

DeveloperAssignment Class (join class):

Code:
@Entity
@Table( name = "PR3_DEVELOPER_ASSIGNMENT" )
public class DeveloperAssignment extends BaseRelationEntity<StoryAssignment, Developer, DeveloperAssignmentPK>
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  @AttributeOverride( name = "developerId", column = @Column( name = "DEVELOPER_ID" ) )
  protected DeveloperAssignmentPK primaryKey;

  @SuppressWarnings( "unused" )
  @ManyToOne( optional = false )
  @PrimaryKeyJoinColumn( name = "STORY_ASSIGNMENT_ID", referencedColumnName = "STORY_ASSIGNMENT_ID" )
  private StoryAssignment storyAssignment;

  @ManyToOne( optional = false )
  @PrimaryKeyJoinColumn( name = "DEVELOPER_ID", referencedColumnName = "DEVELOPER_ID" )
  private Developer developer;


The DeveloperAssignmentPK class:
Code:
@Embeddable
public class DeveloperAssignmentPK extends BasePrimaryKey
{

  @Column( name = "STORY_ASSIGNMENT_ID" )
  protected Integer storyAssignmentId;

  @Column( name = "DEVELOPER_ID" )
  protected Integer developerId;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 5:13 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
The equals and HashCode method were defined in the PK Super Class

this is very bad practice, how can you gaurantee simmetry of the equals function?

I tried your mapping, the schema is fine. what are you trying to do when the exception is thrown?
stacktrace?
Also I couldn't extend your superclasses, is there anything special there?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 8:39 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
I am using the hashcode builders from apache commons lang.
BasePrimaryKey Class:
Code:
@MappedSuperclass
public abstract class BasePrimaryKey implements Serializable
{
  @Column( name = "START_DAT" )
  protected Timestamp start;

  @Column( name = "END_DAT" )
  protected Timestamp end;

  /**
   * Constructor.
   */
  public BasePrimaryKey()
  {
    super();
  }

  /**
   * @param start
   * @param end
   */
  public BasePrimaryKey( Timestamp start, Timestamp end )
  {
    this();
    this.start = start;
    this.end = end;
  }

  // @see java.lang.Object#equals(java.lang.Object)
  @Override
  public boolean equals( Object obj )
  {
    return EqualsBuilder.reflectionEquals( this, obj );
  }

  // @see java.lang.Object#hashCode()
  @Override
  public int hashCode()
  {
    return HashCodeBuilder.reflectionHashCode( this );
  }


The BaseRelationEntity Class (Nothing special here)
Code:
* @param <P> The parent of a relationship
* @param <C> The child of a relationship
* @param <K> The primary key
*/
@MappedSuperclass
public abstract class BaseRelationEntity<P extends BaseEntity, C extends BaseEntity, K extends BasePrimaryKey> implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Override
  public String toString()
  {
    return ToStringBuilder.reflectionToString( this );
  }

  /**
   * Gets the primary key.
   * @return The primary key.
   */
  public abstract K getPrimaryKey();

  /**
   * Sets the primary key.
   * @param primaryKey The primary key
   */
  public abstract void setPrimaryKey( K primaryKey );

  /**
   * Gets the parent of a relationship.
   * @return The parent of a relationship
   */
  public abstract P getParent();

  /**
   * Sets the parent of a relationship.
   * @param parent The parent of a relationship
   */
  public abstract void setParent( P parent );


Stacktrace:
Code:
javax.persistence.PersistenceException: [PersistenceUnit: ProtoPuTest] Unable to build EntityManagerFactory
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
   at ch.ipi.proto.base.PersistenceBaseStandalone.createServices(PersistenceBaseStandalone.java:49)
   at ch.ipi.proto.impl.ProtoBeanDBTest.setUpClass(ProtoBeanDBTest.java:30)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
   at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
   at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.HibernateException: Missing column: developer_DEVELOPER_ID in ESVPROTO.PR3_DEVELOPER_ASSIGNMENT
   at org.hibernate.mapping.Table.validateColumns(Table.java:254)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1093)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:326)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1304)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:854)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
   ... 17 more


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 11:49 am 
Newbie

Joined: Fri Aug 29, 2008 7:28 am
Posts: 3
This seems to be exactly the same problem I'd been getting, see: http://forum.hibernate.org/viewtopic.php?t=990107

The workaround I used was to use @JoinColumn with updates and inserts disabled, instead of @PrimaryKeyJoinColumn. However, if anyone knows what the real solution is, I'd really appreciate it.


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.