I have three tables with the following structure
CREATE TABLE users (
ID INT AUTO_INCREMENT,
FIRSTNAME VARCHAR(100) NOT NULL,
LASTNAME VARCHAR(100) NOT NULL,
AGE INT NOT NULL,
ID_ROLE INT NOT NULL,
ID_LEVEL INT NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE roles (
ID INT AUTO_INCREMENT,
ROLE VARCHAR(100) NOT NULL,
);
CREATE TABLE levels (
ID INT AUTO_INCREMENT,
LEVEL VARCHAR(100) NOT NULL,
);
The users table is related to the roles and levels tables through foreign key ID_ROLE and ID_LEVEL (Both refer to the ID fields). In Java I have three classes that I would mapping on these tables with Hibernate. I using the XML configuration file. The three classes look like this:
public class User {
int id, age;
private String firstname, lastname;
private Role ref_role;
private Level ref_level;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstname;
}
public void setFirstName(String firstname) {
this.firstname= firstname;
}
public String getLastName() {
return lastname;
}
public void setLastName(String lastname) {
this.lastname= lastname;
}
public Ruolo getRef_role() {
return ref_role;
}
public void setRef_role(Role ref_role) {
this.ref_ruolo = ref_ruolo;
}
public Livello getRef_level() {
return ref_level;
}
public void setRef_level(Level ref_level) {
this.ref_level= ref_level;
}
public User() {}
}
public class Role {
private int id;
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Role() {
}
}
public class Level {
private int id;
private String level;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public Level() {}
}
The configuration file of hibernate is follows:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<mapping resource="user.hbm.xml"/>
<mapping resource="role.hbm.xml"/>
<mapping resource="level.hbm.xml"/>
</session-factory>
</hibernate-configuration>
As for the mapping files I have the following files
For User class (in user.hbm.xml file):
<hibernate-mapping>
<class name="User" table="users">
<id name="id" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="firstname" column="FIRSTNAME" type="string"/>
<property name="lastname" column="LASTNAME" type="string"/>
<property name="age" column="AGE" type="integer"/>
<many-to-one name="ref_role" class="Role" column="ID_ROLE" not-null="true"/>
<many-to-one name="ref_level" class="Level" column="ID_LEVEL" not-null="true"/>
</class>
</hibernate-mapping>
The Role class mapping is in role.hbm.xml file:
<hibernate-mapping>
<class name="Role" table="roles">
<id name="id" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="role" column="ROLE" type="string"/>
</class>
</hibernate-mapping>
The Level class mapping is in level.hbm.xml file:
<hibernate-mapping>
<class name="Level" table="levels">
<id name="id" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="level" column="LEVEL" type="string"/>
</class>
</hibernate-mapping>
It get me problem with two many-to-one relationship, whereas with only one it work fine. With two many-to-one relationship the application throw me the follow exception.
org.hibernate.PropertyValueException: not-null property references a null or transient value : model.User.ref_level
Do you know how I can solve it and why does it give me this exception?