org.hibernate.TransientPropertyValueException

时间:2016-11-16 14:00:24

标签: java xml hibernate

我错了什么,我该怎么办?

当我尝试将对象插入数据库时​​,我在编译期间有一个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;
}}

用户类

@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

1 个答案:

答案 0 :(得分:0)

你的程序可能有很多错误。至少这些错误我可以指出:

new BufferedReader(new InputStreamReader(new FileInputStream("my/path/to/File"), "UTF-8"));

(1)对象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); (类role的一个实例)没有Role

(2)对象role_iduser1没有user2

(3)在保存role_idRole之前,user1的记录必须很少,因为这些对象需要外键(user2)。