gpt4 book ai didi

java - 无法使用 Hibernate 3.x 框架创建表并在表中插入数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:19 25 4
gpt4 key购买 nike

我是 Hibernate 新手,正在自学。我在执行程序时遇到问题。我已经尝试了很多方法来解决该错误,但没有成功。这是我的主文件和配置文件以及 hbm 文件。

package com.HibernateLearn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Client {
public static void main(String[] args) {

// creating configuration object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file

// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();

// creating session object
Session session = factory.openSession();

// creating transaction object
Transaction t = session.beginTransaction();

Employee e1 = new Employee();
e1.setId(103);
e1.setF_name("Ahammed");
e1.setL_name("Naseer");
//e1.getId();
//e1.getF_name();
e1.getL_name();
session.persist(e1);// persisting the object

t.commit();// transaction is committed
session.close();

System.out.println("successfully saved");

}
}

我的 hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<!-- <property name="hbm2ddl.auto">create-update</property> -->
<!--Below are other values for hbm2ddl.auto validate: validate the schema,
makes no changes to the database. update: update the schema. create: creates
the schema, destroying previous data. create-drop: drop the schema at the
end of the session. -->
<!--property name="hibernate.cache.use_query_cache">true</property -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/joctopusdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc123</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我的 hibernate.hbm.xml 文件

<?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="com.HibernateLearn.Employee" table="emp_table">
<id name="Id">
<generator class="increment"></generator>
</id>

<property name="F_name"></property>
<property name="L_name"></property>
</class>
</hibernate-mapping>

我收到错误

Exception in thread "main" org.hibernate.PersistentObjectException: detached entity passed to persist: com.HibernateLearn.Employee

最佳答案

您执行e1.setId(103);,同时为您生成新实体的唯一 ID:

<id name="Id">  
<generator class="increment"></generator>
</id>

Hibernate 期望您的Employee 实例已在数据库中,因为它已设置了id。换句话说,Hibernate 期望它更新附加 session 的现有实例。因此出现分离错误消息。

因此,要解决此问题,您不应自己为新实体设置 id

关于java - 无法使用 Hibernate 3.x 框架创建表并在表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com