UDE_Student
UDE_Student

Reputation: 349

Hibernate mapping exception: Could not determine type for columns

I want to do a mapping between my two classes Customer and CustomerGroup and i am getting this error:

Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException: Could not determine type for: studyproject.CustomerGroup, at table: CUSTOMER, for columns: [org.hibernate.mapping.Column(assignedTo)]

my CustomerGroup Class is: (fyi: at the beginning i don't need the BonusPackage type, therefore i didn't annotate it)

package studyproject;


import javax.persistence.Column;
import javax.persistence.Id;


public class CustomerGroup {
private String name;
private String description;
private int id;
private BonusPackage bonusPackage;

public CustomerGroup(int id,String name, String description,
        BonusPackage bonusPackage) {
    super();
    this.id=id;
    this.name = name;
    this.description = description;
    this.bonusPackage = bonusPackage;
}

@Column(name="id")
public int getId() {
    return id;
}

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

@Column(name="name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name="description")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
@Transient
public BonusPackage getBonusPackage() {
    return bonusPackage;
}

public void setBonusPackage(BonusPackage bonusPackage) {
    this.bonusPackage = bonusPackage;
}
@Transient
public boolean generateServiceMails() {
    return false;
}

the customer class:

@Entity
@Table(name="CUSTOMER")

public class Customer {
private int id;
private String forename;
private String surname;
private char gender;
private Date birthday;
private double generatedProfitsTotal;
private double generatedProfitsLastYear;    
private CustomerGroup assignedTo;

public Customer(int id, String forename, String surname, char gender,
        Date birthday) {
    super();
    this.id = id;
    this.forename = forename;
    this.surname = surname;
    this.gender = gender;
    this.birthday = birthday;
}

@Id
public int getId() {
    return id;
}

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

@Column(name = "forename")
public String getForename() {
    return forename;
}

public void setForename(String forename) {
    this.forename = forename;
}
@Column(name = "surename")
public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}
@Column(name = "gender")
public char getGender() {
    return gender;
}

public void setGender(char gender) {
    this.gender = gender;
}
@Column(name = "birthday")
public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
@Column(name = "generatedProfitsTotal")
public double getGeneratedProfitsTotal() {
    return generatedProfitsTotal;
}

public void setGeneratedProfitsTotal(double generatedProfitsTotal) {
    this.generatedProfitsTotal = generatedProfitsTotal;
}
@Column(name = "generatedProfitsLastYear")
public double getGeneratedProfitsLastYear() {
    return generatedProfitsLastYear;
}

public void setGeneratedProfitsLastYear(double generatedProfitsLastYear) {
    this.generatedProfitsLastYear = generatedProfitsLastYear;
}


@OneToOne(fetch = FetchType.LAZY, mappedBy = "id", cascade = CascadeType.ALL)
public CustomerGroup getAssignedTo() {
    return assignedTo;
}

public void setAssignedTo(CustomerGroup assignedTo) {
    this.assignedTo = assignedTo;
}

The method that commits the transaction:

public String saveAllDataORM(){

        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
        applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        for(Customer c:this.getCustomers()) {
            session.persist(c);

        }
    transaction.commit();
            session.close();

        return "Done";
    }

my customer.hbm.xml file

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="studyproject.Customer"
 table="CUSTOMER">
 <id name="id" type="int">
 <column name="ID"/>
 <generator class="assigned"/>
 </id>

 <property name="forename" type="java.lang.String">
 <column name="FORENAME"/>
 </property>

  <property name="surname" type="java.lang.String">
 <column name="SURNAME"/>
 </property>

 <property name="gender" type="java.lang.Character">
 <column name="GENDER"/>
 </property>

  <property name="birthday" type="java.util.Date">
 <column name="birthday"/>
 </property>

 property name="generatedProfitsTotal" type="java.lang.Double">
 <column name="generatedProfitsTotal"/>
 </property>

 <property name="generatedProfitsLastYear" type="java.lang.Double">
 <column name="generatedProfitsLastYear"/>
 </property>

 <property name = "assignedTo" type ="studyproject.CustomerGroup">
            <column name="assignedTo" />

 </property>



 </class>
</hibernate-mapping>

I checked several FAQs and other Threads here at stackoverflow, mostly there was problems with annotations. I tried to fix it in my code but i can't accomplish.. :/

for better illustration i have an UML here: UML

Upvotes: 0

Views: 2830

Answers (2)

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

Assuming class CustomerGroup reprsents CustomerGroup table in database, add below mentioned annotations:

@Entity
@Table(name="CustomerGroup")
public class CustomerGroup

Upvotes: 1

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23562

You have to tell Hibernate which kind of association you want between Customer and CustomerGroup: one-to-one, or many-to-one.

Also, by default, all properties are mapped by JPA standard (that's why Hibernate has to adhere to it if you use JPA annotations).

If you want to make a property transient, you have to explicitly do it:

@Transient
public BonusPackage getBonusPackage() {
    return bonusPackage;
}

Upvotes: 0

Related Questions