2016-11-16 13 views
0

我錯了什麼,我該怎麼辦?org.hibernate.TransientPropertyValueException

我有編譯過程中org.hibernate.TransientPropertyValueException exeption當我嘗試將對象插入我的數據庫:

主類

public class Main { 
public static void main(String[] args) { 
    SessionFactory sessionFactory = new HibernateUtil().getSessionFactory(); 
    Session session = sessionFactory.openSession(); 
    session.beginTransaction(); 

    Role role = new Role(); 
    role.setTitle("ex-president"); 

    User user1 = new User(100,"Barack","Obama"); 
    User user2 = new User(100,"Ronald","Reagan"); 

    user1.setRole(role); 
    user2.setRole(role); 

    session.save(user1); 
    session.save(user2); 

    role.getUsers().add(user1); 
    role.getUsers().add(user2); 

    session.save(role); 

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

HibernateUtil類

public class HibernateUtil { 
private static SessionFactory sessionFactory; 

static { 
    Configuration configuration = new Configuration(); 
    configuration.configure("hibernate.cfg.xml"); 
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); 
    sessionFactory = configuration.buildSessionFactory(ssrb.build()); 
} 

public SessionFactory getSessionFactory(){ 
    return sessionFactory; 
}} 

角色類

@Entity 
public class Role { 
private Long role_id; 

private String title; 

private Set<User> users = new HashSet<User>(); 

public Role(){} 

public Set<User> getUsers() { 
    return users; 
} 

public void addUser(User user){ 
    users.add(user); 
} 

public void setUsers(Set<User> users) { 
    this.users = users; 
} 

public Long getRole_id() { 
    return role_id; 
} 

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

public String getTitle() { 
    return title; 
} 

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

User類

@Entity 
public class User { 

private long user_id; 

private int age; 

private String firstname; 

private String lastname; 

private Role role; 

public User(){ 

} 

public Role getRole() { 
    return role; 
} 

public void setRole(Role role) { 
    this.role = role; 
} 

public User(int age, String firstname, String lastname) { 
    this.age = age; 
    this.firstname = firstname; 
    this.lastname = lastname; 
} 

public long getUser_id() { 
    return user_id; 
} 

public void setUser_id(long id) { 
    this.user_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; 
}} 

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
<property name="connection.username">root</property> 
<property name="connection.password">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hbm2ddl.auto">create</property> 
    <property name="show_sql">true</property> 
    <property name="format_sql">true</property> 
    <mapping resource="user.cfg.xml"/> 
<mapping resource="role.cfg.xml"/> 
</session-factory> 
</hibernate-configuration> 

role.cfg.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="Role" table="role"> 
    <id name="role_id" column="id"> 
     <generator class="native"/> 
    </id> 
    <property name="title" column="TITLE"/> 
    <set name="users" cascade="all" 
     inverse="true" lazy="true" fetch="select"> 
     <key column="id" not-null="true"/> 
     <one-to-many class="User"/> 
    </set> 
</class> 
</hibernate-mapping> 

user.cfg.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="User" table="user"> 
<id name="user_id" column="user_id"> 
    <generator class="native"/> 
</id> 
<property name="firstname" column="firstname"/> 
<property name="lastname" column="lastname"/> 
<property name="age" column="age"/> 
<many-to-one name="role" class="Role" fetch="select"> 
    <column name="id" not-null="true"/> 
</many-to-one> 
</class> 
</hibernate-mapping> 

在日誌中的錯誤信息: enter image description here

回答

0

你的程序可能有很多錯誤。至少這些錯誤,我可以指出:

Role role = new Role(); 
role.setTitle("ex-president"); 
User user1 = new User(100, "Barack", "Obama"); 
User user2 = new User(100, "Ronald", "Reagan"); 
user1.setRole(role); 
user2.setRole(role); 
session.save(user1); 
session.save(user2); 
role.getUsers().add(user1); 
role.getUsers().add(user2); 
session.save(role); 

(1)對象roleRole類的一個實例)尚未role_id

(2)對象user1user2還沒有role_id

(3)必須具備幾個Role記錄之前保存user1user2,因爲這些對象需要外鍵(role_id)。

相關問題