-->
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: Association class with compound PK with synthetic FKs
PostPosted: Wed Jun 07, 2006 2:27 pm 
Newbie

Joined: Wed Jun 07, 2006 11:20 am
Posts: 2
I have read the docs, searched and read the FAQ. If the solution to this is obvious it is because I am thick, not lazy.

I am having trouble mapping an association class to a table that has a compound key that includes two synthetic foreign keys. When I attempt to deploy the mapping I get an exception telling me that it expects the column type of person_id (one of the synthetic FKs that makes up the PK of the association table) to be of type varbinary(255). This leads me to believe that it is trying to serialize the reference type (Person) rather than store the FK but I am stuck as to how to fix it.

Hibernate version: 3.2.0.cr2 (packaged with JBoss 4.0.4 GA EJB3 deployment.

Mapping documents (persistence.xml):
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="entityManager" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/PPIDS-XA</jta-data-source>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="validate "/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.cache.use_query_cache" value="true"/>
         <property name="jboss.entity.manager.factory.jndi.name" value="java:/testEntityManagerFactory"/>
         <property name="hibernate.jdbc.charSet" value="utf-8"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
      </properties>
   </persistence-unit>
</persistence>


Code between sessionFactory.openSession() and session.close():

The association class, Ownership.java:
Code:
@Entity
@Table(name="AnimalPerson")
public class Ownership implements Serializable {
 
  @EmbeddedId
  private OwnershipPK pk;
 
  @ManyToOne 
  @JoinColumn(name="owner", insertable=false, updatable=false)
  public Animal owned;

  @ManyToOne
  @JoinColumn(name="person_id",insertable=false, updatable=false)
  private Person person_id;
 
  @Column(name="person_role",insertable=false, updatable=false)
  private int ownerRole;
 
 
  @Column(name="date_added")
  private Date added;

  //snip setters, getters, equals() and hashCode()
}



The association class' key (OwnershipPK.java):
Code:
@Embeddable
public class OwnershipPK implements Serializable {
  public Person person_id;
  public Animal animal_id;
  public int person_role;

  //snip hashCode() and equals()
}


Person.java (one side of the association):
Code:
@Entity
@Table(name="Person")
public class Person implements Serializable{
  @Id
  @Column(name="person_id")
  protected long id;
 
  @OneToMany(mappedBy="person_id")
  private Set<Ownership> ownerships;

  //snip extraneous fields, setters, getters, equals() and hashCode()
}


Animal.java (the other side of the association):
Code:
@Entity
@Table(name="Animal")
public class Animal implements Serializable {
  @Id 
  @Column(name="animal_id")
  protected long id;
 
  @OneToMany(mappedBy="animal")
  private Set<TransferActivation> pendingActivations;

  @OneToMany(mappedBy="owned")
  private Set<Ownership> ownership;
 
//snip extraneous fields, setters, getters, equals() and hashCode()
}


Full stack trace of any exception that occurs:
Code:
14:11:35,765 WARN  [ServiceController] Problem starting service persistence.units:ear=BHIA2.ear,jar=BHIA2-ejbs.jar,unitName=entityManager
org.hibernate.HibernateException: Wrong column type: person_id, expected: varbinary(255)
   at org.hibernate.mapping.Table.validateColumns(Table.java:251)
   at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1002)
   at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1213)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
   at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
   at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:350)
   at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:119)
   at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
   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:585)
   at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:99)
   at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
   at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
   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:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
   at $Proxy0.start(Unknown Source)
   at org.jboss.system.ServiceController.start(ServiceController.java:417)
   at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
   at $Proxy50.start(Unknown Source)
   at org.jboss.ejb3.JmxKernelAbstraction.install(JmxKernelAbstraction.java:82)
   at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:626)
   at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:475)
   at org.jboss.ejb3.Ejb3Module.startService(Ejb3Module.java:139)
   at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
   at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
   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:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
   at $Proxy0.start(Unknown Source)
   at org.jboss.system.ServiceController.start(ServiceController.java:417)
   at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
   at $Proxy34.start(Unknown Source)
   at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:428)
   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:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
   at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
   at org.jboss.mx.interceptor.DynamicInterceptor.invoke(DynamicInterceptor.java:97)
   at org.jboss.system.InterceptorServiceMBeanSupport.invokeNext(InterceptorServiceMBeanSupport.java:238)
   at org.jboss.ws.server.WebServiceDeployer.start(WebServiceDeployer.java:117)
   at org.jboss.deployment.SubDeployerInterceptorSupport$XMBeanInterceptor.start(SubDeployerInterceptorSupport.java:188)
   at org.jboss.deployment.SubDeployerInterceptor.invoke(SubDeployerInterceptor.java:95)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
   at $Proxy35.start(Unknown Source)
   at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1007)
   at org.jboss.deployment.MainDeployer.start(MainDeployer.java:997)
   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:808)
   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:771)
   at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
   at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
   at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
   at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
   at $Proxy6.deploy(Unknown Source)
   at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:421)
   at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:634)
   at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:263)
   at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:274)
   at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:225)

Name and version of the database you are using: SQL Server 2000

Any help would be greatly appreciated.

Thank You,
Victor


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 4:06 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
I'm not 100%, but here is a thought. In your primary key, you write:

Quote:
@Embeddable
public class OwnershipPK implements Serializable {
public Person person_id;
public Animal animal_id;
public int person_role;

//snip hashCode() and equals()
}


I think you should either use those values as longs (this is how I did something like this in my app), or you might actually need to mark those fields (person_id and animal_id) as @ManyToOne, e.g. being relationships. Being a relationship is what makes a field store the foreign key instead of the object itself.

Another thing, I'm not sure if it causes any problems (your current exception has nothing to do with this), but the docs say:
Quote:
You can define a composite primary key through several syntaxes:

* annotate the component property as @Id and make the component class @Embeddable
* annotate the component property as @EmbeddedId
* annotate the class as @IdClass and annotate each property of the entity involved in the primary key with @Id


So you might want to use the @Id annotation instead of @EmbeddedId, if your primary key is already annotated @Embedded.

Again, this might not be a problem, but @Id - @Embedded combination works for me fine.

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 6:08 pm 
Newbie

Joined: Wed Jun 07, 2006 11:20 am
Posts: 2
Roland,

Thank you! It was putting the @ManyToOne on the FKs in the composite PK that did it. I very much appreciate you help!

-Victor


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.