AndreaNobili
AndreaNobili

Reputation: 43057

Difficulties to use Hibernate in a Spring: Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:

I am new in Spring ORM and I am trying to implement this Spring+Hibernate tutorial:

http://it4beginners.wordpress.com/2012/10/05/spring-3-and-hibernate-4-for-beginners/

The only difference is that I am not using PostgreSQL but I am using MySql databse, so I have insert the right MySql connector in the pom.xml file and I have change the mainDataSource property in the ApplicationContext.xml configuration file...

The problem is that when I run my App.java class (the main class that contain the main method) I obtain the following error message in the stacktrace:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: org.andrea.myexample.myHibernateSpringExample.entityclasses.ChemicalStructure.occurence[java.lang.Object]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1057)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1597)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 12 more

I don't understand where is the problem !!!

This is the code of the class that seems present the problem:

package org.andrea.myexample.myHibernateSpringExample.entityclasses;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "chemical_structure")
public class ChemicalStructure implements Serializable {

private Long id;
private String structureKey;
private String structureData;
private Set<Object> occurence;

/**
 * @return the id
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "chem_structure_seq_gen")
@SequenceGenerator(name = "chem_structure_seq_gen", sequenceName = "seq_chemical_structure")
@Column(name = "structure_id")
public Long getId() {
    return id;
}

/**
 * @param id
 *            the id to set
 */
public void setId(Long id) {
    this.id = id;
}

/**
 * @return the structureKey
 */
@Column(name = "structure_key", unique = true)
public String getStructureKey() {
    return structureKey;
}

/**
 * @param structureKey
 *            the structureKey to set
 */
public void setStructureKey(String structureKey) {
    this.structureKey = structureKey;
}

/**
 * @return the structureData
 */
@Column(name = "chemical_structure")
public String getStructureData() {
    return structureData;
}

/**
 * @param structureData
 *            the structureData to set
 */
public void setStructureData(String structureData) {
    this.structureData = structureData;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.chemicalStructure", cascade = CascadeType.ALL)
public Set<Object> getOccurence() {
    return occurence;
}

public void setOccurence(Set<Object> occurence) {
    this.occurence = occurence;
}
}

Do you have some idea?

Tnx

Upvotes: 0

Views: 820

Answers (2)

Qkyrie
Qkyrie

Reputation: 939

The reason for that error lies in the fact that you try to map a Set of plain Objects. Object is not a JPA-entity and therefore it cannot be mapped to a relational type.

You'll need to create an entity named Occurence and map it the way you did it with the Set of Objects.

Upvotes: 1

Caron
Caron

Reputation: 96

I think the problem is here mappedBy = "pk.chemicalStructure" Check that you have a ManyToOne relation in this class like:

@ManyToOne
@JoinColumn(name = "chemicalstructure_id", nullable = false)
@ForeignKey(name = "fk_chemicalstructure_id")
private Occurence occurence;

then mappedBy = "occurence"

The error says that this bi directional relation is missing.

Upvotes: 0

Related Questions