views:

872

answers:

5

hi, i have a webapplication, using hibernate and the following entities. What do I wrong:

...
@Entity
public class Registration implements BaseEntity {
...
@OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "registration", fetch = FetchType.EAGER)
private List<OrderedProduct> orderedProducts = new ArrayList<OrderedProduct>();
...
public List<OrderedProduct> getOrderedProducts() {
    return orderedProducts;
}

public void setOrderedProducts(List<OrderedProduct> orderedProducts) {
    this.orderedProducts = orderedProducts;
}
...


...
@Entity
public class OrderedProduct implements BaseEntity {
...
@ManyToOne
@JoinColumn(name = "registration_id",nullable = true)
private Registration registration;
...
public Registration getRegistration() {
    return registration;
}
public void setRegistration(Registration registration) {
    this.registration = registration;
}
...


when I use this, I get following Tomcat Error:

Caused by: org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: at.irian.conference.domain.Registration.orderedProducts at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:193) at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1325) at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1164) at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:602) at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543) at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148) at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226) at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:425) at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:221) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1288) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1257) ... 86 more

A: 

One of them needs an "inverse" annotation. Oops, never mind, that's for many-to-many associations.

Jason S
A: 

Mapping collection with List needs a "sequence" column mapped in the database. Did you intend to use as Set

Surya
+1  A: 

this code is right, i have forgotten to delete a other relationship

ddejmek
you should accept this answer if the question is no longer open.
Nathan Feger
A: 

Am not sure of the syntax using Annotations. We used to define such a relation like below.

Registration.hbm.xml

<bag
 name="orderedProducts"
 inverse="true"
 cascade="appropriate-one"
>
 <key>
  <column name="REGISTRATION_ID" />
 </key>
 <one-to-many 
  class="orderedProducts"
 />
</bag>

OrderedProducts.hbm.xml

<many-to-one
 name="registration"
 class="Registration"
>
 <column name="REGISTRATION_ID"/>
</many-to-one>
lud0h
A: 

Try the annotations like as below. It will solve ur problems @ManyToOne(fetch = FetchType.EAGER)

@JoinColumn(name = "registration_id")

public Registration getRegistration() { return registration; } public void setRegistration(Registration registration) { this.registration = registration; }

@OneToMany(fetch = FetchType.EAGER, targetEntity = Registration.class, mappedBy = "registration", cascade = { CascadeType.ALL })
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "registration_id")
Ashok