Hello,
I'm currently using ejb3 in order to handle our new database model and i'm encoutering mainly two issues. Note that i'm using the RC8 embaddable ejb3 persistance with the simple example for launch my model creation.
The first one resides on how to specify correctly the bidirectional property to use in a @OneToMany relationship. I have set up the "mappedBy" property successfuly for many relations but i get stuck when i have an EmbeddedId.
Here is a simplified example of my classes :
Code:
@Entity
public class Card {
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="cardtmp")
private Set<CardField> fields;
Code:
@Entity
public class CardField implements Field {
@EmbeddedId
private PrimaryKey primaryKey = new PrimaryKey();
@Embeddable
class PrimaryKey implements Serializable {
@ManyToOne(optional = false)
private Card card;
@ManyToOne(optional = false)
private CardKey key;
}
//A trick in order to use the mappedBy solution
@ManyToOne
private Card cardtmp;
}
Indeed, i would like to specify something like primaryKey.card in the mappedBy field, but it doesn't work. I saw in the entity reference it's possible to use something like @JoinColumn(name="troop_fk", insertable=false, updatable=false) but i was wondering if it was possible to use the mappedBy notation who look more conveniant to me ?
Code:
.
My second issue is a bit more tricky and i think it might be a bug in the hibernate implementation but i prefer to expose it here before post anything on JIRA. The @Id of my Card object is also composed with and EmbeddedId and I got an exception when hibernate try to build my model depending on packages names.
Supposing hibernate load classes by package alphabetical order; if it get the class Card first all works fine, if it get the class CardField first, it raise an exception !
Here is a subset of my classes, cleaned of getters, setters and all unrelated things :
Code:
package fr.ifis.entity.field.card;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import fr.ifis.entity.project.Card;
@Entity
public class CardField {
@Id
private PrimaryKey primaryKey = new PrimaryKey();
// A trick in order to use the mappedBy solution
@ManyToOne
private Card cardtmp;
@Embeddable
private class PrimaryKey implements Serializable {
@ManyToOne(optional = false)
private Card card;
@ManyToOne(optional = false)
private CardKey key;
}
}
Code:
package fr.ifis.entity.field.card;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class CardKey {
@Id
@GeneratedValue
private int id;
}
Code:
package fr.ifis.entity.project;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import fr.ifis.entity.field.card.CardField;
@Entity
public class Card {
@Id
private CardPrimaryKey primaryKey = new CardPrimaryKey();
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="cardtmp") //
private Set<CardField> fields;
@Embeddable
private class CardPrimaryKey implements Serializable {
@ManyToOne(optional = false)
private Project project;
//An other @ManyToOne is also present in the real model
//The problem still occurs even when i remove this relation, it was no use to kept it for describe the problem
}
}
Code:
package fr.ifis.entity.project;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Project {
@Id
@GeneratedValue
private int id;
}
The exception stack :
Code:
[java] ERROR 14-06 18:17:45,289 (AbstractController.java:incrementState:350) -Error installing to Start: name=persistence.units:unitName=Refonte state=Create
[java] org.hibernate.MappingException: Foreign key (FK6771BFAA1845E8B:CardField [])) must have same number of columns as the referenced primary key (Card [project_id])
[java] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
[java] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
[java] at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1182)
[java] at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1089)
[java] at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:302)
[java] at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1034)
[java] at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1015)
[java] at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
[java] at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:751)
[java] at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:350)
[java] at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:119)
[java] at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:585)
[java] at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:55)
[java] at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:107)
[java] at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions.dispatchJoinPoint(KernelControllerContextActions.java:100)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$LifecycleAction.installAction(KernelControllerContextActions.java:582)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$KernelControllerContextAction.install(KernelControllerContextActions.java:175)
[java] at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
[java] at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:226)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:593)
[java] at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:346)
[java] at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:438)
[java] at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:379)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:225)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:151)
[java] at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.java:79)
[java] at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.java:73)
[java] at org.jboss.ejb3.MCKernelAbstraction.install(MCKernelAbstraction.java:91)
[java] at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:626)
[java] at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:475)
[java] at org.jboss.ejb3.embedded.EJB3StandaloneDeployer.start(EJB3StandaloneDeployer.java:460)
[java] at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:291)
[java] at org.jboss.tutorial.simple.Main.main(Main.java:41)
[java] WARN 14-06 18:17:45,318 (Log4JLogger.java:warn:104) -Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
[java] ERROR 14-06 18:17:46,607 (KernelErrors.java:validate:74) -*** DEPLOYMENTS IN ERROR:
[java] ERROR 14-06 18:17:46,609 (KernelErrors.java:validate:78) -Failed deployment: persistence.units:unitName=Refonte
[java] org.hibernate.MappingException: Foreign key (FK6771BFAA1845E8B:CardField [])) must have same number of columns as the referenced primary key (Card [project_id])
[java] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
[java] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
[java] at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1182)
[java] at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1089)
[java] at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:302)
[java] at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1034)
[java] at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1015)
[java] at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
[java] at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:751)
[java] at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:350)
[java] at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:119)
[java] at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:585)
[java] at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:55)
[java] at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:107)
[java] at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions.dispatchJoinPoint(KernelControllerContextActions.java:100)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$LifecycleAction.installAction(KernelControllerContextActions.java:582)
[java] at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$KernelControllerContextAction.install(KernelControllerContextActions.java:175)
[java] at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
[java] at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:226)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:593)
[java] at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:346)
[java] at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:438)
[java] at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:379)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:225)
[java] at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:151)
[java] at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.java:79)
[java] at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.java:73)
[java] at org.jboss.ejb3.MCKernelAbstraction.install(MCKernelAbstraction.java:91)
[java] at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:626)
[java] at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:475)
[java] at org.jboss.ejb3.embedded.EJB3StandaloneDeployer.start(EJB3StandaloneDeployer.java:460)
[java] at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:291)
[java] at org.jboss.tutorial.simple.Main.main(Main.java:41)
[java] Exception in thread "main" java.lang.RuntimeException: Problems scanning classpath
[java] at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:298)
[java] at org.jboss.tutorial.simple.Main.main(Main.java:41)
[java]
Indeed, depending on the package names i get or not an excpetion !
It doesn't work with the package fr.ifis.entity.project but if I rename the package in fr.ifis.entity.aproject and then move it over the field.card package, all works fine.
Shall i post this issue on JIRA ?
Regards,
Pierre.