Could not define uni-directional @OneToMany mapping [message #650966] |
Thu, 27 January 2011 10:43 |
Weber Chu Messages: 14 Registered: October 2010 |
Junior Member |
|
|
The Parent class define the @OneToMany mapping to Child class, but Child class will not define any mapping back to Parent class.
Is uni-directional mapping allowed in EclipseLink? Because this is allowed in Hibernate.
The following exception will be thrown:
Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [method getChildSet] from the entity class [class Parent] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
Here is our parent entity:
@Entity
public class Parent {
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true, targetEntity=Child.class)
@JoinColumns({
@JoinColumn(name="col1", referencedColumnName="col1", updatable=false),
@JoinColumn(name="col2", referencedColumnName="col2", updatable=false) })
public Set<Child> getChildSet() {
return this.childSet;
}
}
|
|
|
|
Re: Could not define uni-directional @OneToMany mapping [message #651151 is a reply to message #651029] |
Fri, 28 January 2011 02:55 |
Weber Chu Messages: 14 Registered: October 2010 |
Junior Member |
|
|
Thanks for your suggestion. But our DB column name are all small letters.
Here are the entities I tested.
Parent class:
@Entity
@Table(name = "ClassA")
@Cacheable
public class ClassA implements Serializable{
private static final long serialVersionUID = -1535466991451378483L;
private Map<Object, Object> fieldMap = new HashMap<Object, Object>();
public void setId(String value) {
fieldMap.put("id", value);
}
@Id
@Column(name = "id")
public String getId() {
return (String) fieldMap.get("id");
}
public void setCode(String value) {
fieldMap.put("code", value);
}
@Column(name = "code")
public String getCode() {
return (String) fieldMap.get("code");
}
public void setName(String value) {
fieldMap.put("name", value);
}
@Column(name = "name")
public String getName() {
return (String) fieldMap.get("name");
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true, targetEntity=ClassB.class)
@JoinColumns({
@JoinColumn(name="id", referencedColumnName="id", updatable=false) })
public List<ClassB> getListClassB() {
return (List<ClassB>) fieldMap.get("listClassB");
}
public void setListClassB(List<ClassB> value) {
fieldMap.put("listClassB", value);
}
}
Child class:
@Entity
@Table(name = "ClassB")
@Cacheable
public class ClassB implements Serializable {
private static final long serialVersionUID = -7306594226829492246L;
private Map<Object, Object> fieldMap = new HashMap<Object, Object>();
public void setId(String value) {
fieldMap.put("id", value);
}
@Id
@Column(name = "id")
public String getId() {
return (String) fieldMap.get("id");
}
public void setCoName(String value) {
fieldMap.put("coName", value);
}
@Id
@Column(name = "co_name")
public String getCoName() {
return (String) fieldMap.get("coName");
}
public void setPrice(BigDecimal value) {
fieldMap.put("price", value);
}
@Column(name = "price")
public BigDecimal getPrice() {
return (BigDecimal) fieldMap.get("price");
}
}
|
|
|
|
Re: Could not define uni-directional @OneToMany mapping [message #651911 is a reply to message #650966] |
Wed, 02 February 2011 03:50 |
Weber Chu Messages: 14 Registered: October 2010 |
Junior Member |
|
|
Hi Chris,
I have added ClassBPK as the @IdClass to ClassB. But the problem still exist. Attached please find the exception stack trace.
And an interesting thing we found is, the number of @JoinColumn at ClassA seems have to match the number of @Id at ClassB.
So we write:
@JoinColumns({
@JoinColumn(name="id", referencedColumnName="id", updatable=false),
@JoinColumn(name="id", referencedColumnName="id", updatable=false) })
public List<ClassB> getListClassB() {
return (List<ClassB>) fieldMap.get("listClassB");
})
and this works. Is it a bug?
Here is the exception I got:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at foo.bar.TesterMain.init(TesterMain.java:70)
at foo.bar.TesterMain.main(TesterMain.java:98)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainEmf' defined in class path resource [data-config.xml]: Invocation of init method failed; nested exception is Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@11b86e7
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [mainPU] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [method getListClassB] from the entity class [foo.bar.dao.ClassA] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at foo.bar.SampleTester.<init>(SampleTester.java:56)
... 6 more
Caused by: Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@11b86e7
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [mainPU] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [method getListClassB] from the entity class [class foo.bar.dao.ClassA] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:136)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:65)
at foo.bar.AbstractEntityManagerFactoryWrapper.initEntityManagerFactory(AbstractEntityManagerFactoryWrapper.java:238)
at foo.bar.AbstractEntityManagerFactoryWrapper.afterPropertiesSet(AbstractEntityManagerFactoryWrapper.java:258)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
... 23 more
IdClass for ClassB
public class ClassBPK implements Serializable {
private static final long serialVersionUID = -1151412779028898293L;
private String id;
private String coName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCoName() {
return coName;
}
public void setCoName(String coName) {
this.coName = coName;
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03267 seconds