user3338029
user3338029

Reputation: 13

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class

I am trying to use onetomany mapping for product specifications where I copied similar working code from descriptions. I am providing all my code.

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: sm-unit] Unable to build EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
        ... 38 more
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.salesmanager.core.business.catalog.product.model.Product.specifications[com.salesmanager.core.business.catalog.product.model.specification.ProductSpecification]
        at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1204)

Product.java has entry like this:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product")
    private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>();

ProductSpecification.java has below code..

@Entity
@Table(name = "PRODUCT_SPECIFICATION", schema=SchemaConstant.BEAWLP_JAMBIRA_SCHEMA)
public class ProductSpecification extends SalesManagerEntity<Long, ProductSpecification> {
    private static final long serialVersionUID = -7991123525661321865L;

    @ManyToOne(targetEntity = Product.class)
    @JoinColumn(name = "PRODUCT_ID", nullable = false)
    private Product product;

@Id
@Column(name = "SPECIFICATION_ID", unique = true, nullable = false)
@TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "SPECIFICATION_SEQ_NEXT_VAL")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
private Long id;

@Column(name="TITLE", length=100)
private String title;

public ProductSpecification() {
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

Upvotes: 1

Views: 8453

Answers (4)

Ahmad R. Nazemi
Ahmad R. Nazemi

Reputation: 805

for me, it was fixed by adding class to

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="sample" />
    <property name="annotatedClasses">
        <list>
            <value>******************</value>

Upvotes: 0

Vivien SA&#39;A
Vivien SA&#39;A

Reputation: 767

I noticed that that exception occures when using Set and HashSet. You can instead use List and ArrayList

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product")
private List<ProductSpecification> specifications = new ArrayList<ProductSpecification>();

Upvotes: 0

Suresh karki
Suresh karki

Reputation: 69

From the error message, it can be known that the entity is not mapped properly.

  • Make sure that you have imported javax.persistence.entity and not org.hibernate.annotations.entity.
  • Also, make sure that entity is listed in configuration files (persistence.xml, hibernate.cfg.xml).

Upvotes: 3

Suman Behara
Suman Behara

Reputation: 160

I think you missed targetEntity,try out with below line it may helps you.

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product", targetEntity = ProductSpecification.class)
private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>();

Upvotes: 0

Related Questions