org.hibernate.HibernateException:数据库没有返回使用db mysql生成的本机标识值
这是应用程序hibernate的一个主要示例,我有一个错误,我试图通过在mysql数据库的表中添加自动增量来解决这个问题,但没有改变错误org.hibernate.HibernateException:数据库没有返回使用db mysql生成的本机标识值,hibernate,Hibernate,这是应用程序hibernate的一个主要示例,我有一个错误,我试图通过在mysql数据库的表中添加自动增量来解决这个问题,但没有改变错误 <hibernate-mapping> <class name="Employee" table="EMPLOYEE"> <meta attribute="class-description"> This class cont
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
此类包含员工详细信息。
这是应用程序hibernate的一个主要示例,我有一个错误,我试图通过在mysql数据库的表中添加自动增量来解决这个问题,但没有改变错误
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/anagrafica
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<!-- List of XML mapping files -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ManageEmployee {
private static SessionFactory factory;
public static void main(String[] args) {
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
ManageEmployee ME = new ManageEmployee();
/* Add few employee records in database */
Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 10000);
/* List down all the employees */
ME.listEmployees();
/* Update employee's records */
ME.updateEmployee(empID1, 5000);
/* Delete an employee from the database */
ME.deleteEmployee(empID2);
/* List down new list of the employees */
ME.listEmployees();
}
/* Method to CREATE an employee in the database */
public Integer addEmployee(String fname, String lname, int salary){
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try{
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return employeeID;
}
/* Method to READ all the employees */
public void listEmployees( ){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List employees = session.createQuery("FROM Employee").list();
for (Iterator iterator =
employees.iterator(); iterator.hasNext();){
Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
/* Method to UPDATE salary for an employee */
public void updateEmployee(Integer EmployeeID, int salary ){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Employee employee =
(Employee)session.get(Employee.class, EmployeeID);
employee.setSalary( salary );
session.update(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
/* Method to DELETE an employee from the records */
public void deleteEmployee(Integer EmployeeID){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Employee employee =
(Employee)session.get(Employee.class, EmployeeID);
session.delete(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
}
org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/anagrafica
根
根
公营雇员{
私有int-id;
私有字符串名;
私有字符串lastName;
私人薪酬;
公共雇员(){}
公共雇员(字符串fname、字符串lname、整数工资){
this.firstName=fname;
this.lastName=lname;
这个。薪水=薪水;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串first_name){
this.firstName=名字;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串last_name){
this.lastName=姓氏;
}
public int getSalary(){
返回工资;
}
公共无效设置薪资(内部薪资){
这个。薪水=薪水;
}
}
导入java.util.List;
导入java.util.Date;
导入java.util.Iterator;
导入org.hibernate.hibernateeexception;
导入org.hibernate.Session;
导入org.hibernate.Transaction;
导入org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
公共类经理雇员{
私营静电厂;
公共静态void main(字符串[]args){
试一试{
工厂=新配置().configure().buildSessionFactory();
}捕获(可丢弃的ex){
System.err.println(“未能创建sessionFactory对象。”+ex);
抛出新异常InInitializeRerror(ex);
}
ManageEmployee ME=新的ManageEmployee();
/*在数据库中添加少量员工记录*/
整数empID1=ME.addEmployee(“Zara”,“Ali”,1000);
整数empID2=ME.addEmployee(“Daisy”,“Das”,5000);
整数empID3=ME.addEmployee(“John”、“Paul”,10000);
/*把所有的员工都列下来*/
我是你的雇员;
/*更新员工记录*/
ME.updateEmployee(EMPID15000);
/*从数据库中删除员工*/
ME.deleteEmployee(empID2);
/*列出新的员工名单*/
我是你的雇员;
}
/*方法在数据库中创建员工*/
公共整数addEmployee(字符串fname、字符串lname、整数salary){
Session Session=factory.openSession();
事务tx=null;
整数employeeID=null;
试一试{
tx=session.beginTransaction();
员工=新员工(fname、lname、薪水);
employeeID=(整数)session.save(雇员);
tx.commit();
}捕获(休眠异常e){
如果(tx!=null)tx.rollback();
e、 printStackTrace();
}最后{
session.close();
}
返回员工ID;
}
/*方法读取所有员工*/
公众雇员(){
Session Session=factory.openSession();
事务tx=null;
试一试{
tx=session.beginTransaction();
List employees=session.createQuery(“来自员工”).List();
for(迭代器迭代器=
employees.iterator();iterator.hasNext();){
Employee=(Employee)迭代器.next();
System.out.print(“名字:+employee.getFirstName());
print(“姓氏:”+employee.getLastName());
System.out.println(“Salary:+employee.getSalary());
}
tx.commit();
}捕获(休眠异常e){
如果(tx!=null)tx.rollback();
e、 printStackTrace();
}最后{
session.close();
}
}
/*更新员工工资的方法*/
public void updateEmployee(整数EmployeeID,整数工资){
Session Session=factory.openSession();
事务tx=null;
试一试{
tx=session.beginTransaction();
雇员=
(Employee)session.get(Employee.class,EmployeeID);
员工薪酬(工资);
更新(员工);
tx.commit();
}捕获(休眠异常e){
如果(tx!=null)tx.rollback();
e、 printStackTrace();
}最后{
session.close();
}
}
/*方法从记录中删除员工*/
public void deleteEmployee(整数EmployeeID){
Session Session=factory.openSession();
事务tx=null;
试一试{
tx=session.beginTransaction();
雇员=
(Employee)session.get(Employee.class,EmployeeID);
删除(雇员);
tx.commit();
}捕获(休眠异常e){
如果(tx!=null)tx.rollback();
e、 printStackTrace();
}最后{
session.close();
}
}
}
您忘记在数据库表中使用自动增量。表格定义如下所示的格式
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
CREATE TABLE EMPLOYEE (
ID INT AUTO_INCREMENT,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
SALARY INT,
PRIMARY KEY (ID)
) ENGINE=InnoDB;
您忘记在数据库表中使用自动增量
。这个