需要你的洞察力,期待你的救助。
这是主要方法,尝试使用hibernate将数据保存到db。
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.hibernate.dto.Employee;
public class HibernateTest {
public static void main(String[] args) {
Employee emp = new Employee();
System.out.println("abc");
emp.setFirstName("John");
emp.setLastName("More");
emp.setSalary(999999972);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(emp);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
这是模型类,即。雇员
package com.hibernate.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private int id;
private String firstName;
private String lastName;
private int salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
这是hibernate.cfg.xml。
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/empdb</property>
<property name="connection.username">root</property>
<property name="connection.password">***</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.hibernate.dto.Employee"/>
</session-factory>
</hibernate-configuration>
我所包含的罐子列为:
D:\hibernate-release-5.1.0.Final\lib\required\antlr-2.7.7.jar
D:\hibernate-release-5.1.0.Final\lib\required\classmate-1.3.0.jar
D:\hibernate-release-5.1.0.Final\lib\required\dom4j-1.6.1.jar
D:\hibernate-release-5.1.0.Final\lib\required\geronimo-jta_1.1_spec-1.1.1.jar
D:\hibernate-release-5.1.0.Final\lib\required\hibernate-commons-annotations-5.0.1.Final.jar
D:\hibernate-release-5.1.0.Final\lib\required\hibernate-core-5.1.0.Final.jar
D:\hibernate-release-5.1.0.Final\lib\required\hibernate-jpa-2.1-api-1.0.0.Final.jar
D:\hibernate-release-5.1.0.Final\lib\required\jandex-2.0.0.Final.jar
D:\hibernate-release-5.1.0.Final\lib\required\javassist-3.20.0-GA.jar
D:\hibernate-release-5.1.0.Final\lib\required\jboss-logging-3.3.0.Final.jar
D:\hibernate-release-5.1.0.Final\lib\jpa\hibernate-entitymanager-5.1.0.Final.jar
D:\hibernate-release-5.1.0.Final\lib\java8\hibernate-java8-5.1.0.Final.jar
C:\Users\arpit_pipersaniya\Downloads\javassist-3.12.0.GA.jar
C:\Users\arpit_pipersaniya\Downloads\mysql-connector-java-5.1.18.jar
当我运行这个java应用程序时,没有任何反应,因此没有预期的结果。
答案 0 :(得分:0)
通过调整配置文件,现在它可以正常运行文件。
这就是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>
*<!-- <?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> -->*
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/empdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root2</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.hibernate.dto.Employee"/>
</session-factory>
</hibernate-configuration>