将实例保存到数据库中

时间:2015-04-17 06:10:18

标签: java hibernate maven

我正在网上练习一个现有的JSF + Spring + Hibernate集成示例。这是Employee.java:

package com.hibernate.data;

import java.util.Date;

public class Employee {
private long employeeId;
private String employeeName;
private Date employeeHireDate;
private double employeeSalary;

public long getEmployeeId(){
return employeeId;
}
public void setEmployeeId(long employeeId){
this.employeeId = employeeId;
}
public String getEmployeeName(){
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public Date getEmployeeHireDate() {
return employeeHireDate;
}

public void setEmployeeHireDate(Date employeeHireDate) {
this.employeeHireDate = employeeHireDate;
}

public double getEmployeeSalary() {
return employeeSalary;
}

public void setEmployeeSalary(double employeeSalary) {
this.employeeSalary = employeeSalary;
}
}

这是pom.xml文件:



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.journaldev</groupId>
  <artifactId>Primefaces-Hibernate-Spring-Integration-Sample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Primefaces-Hibernate-Spring-Integration-Sample Maven Webapp</name>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <dependencies>
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!-- Faces Implementation -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.4</version>
        </dependency>
        <!-- Faces Library -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.4</version>
        </dependency>
        <!-- Primefaces Version 5 -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.0</version>
        </dependency>
        <!-- JSP Library -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- JSTL Library -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- Primefaces Theme Library -->
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>blitzer</artifactId>
            <version>1.0.10</version>
        </dependency>
        <!-- Hibernate library -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>
        <!-- MySQL driver connector library -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
    </dependencies>
</project>
&#13;
&#13;
&#13;

hibernate.cfg.xml中:

&#13;
&#13;
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
    <session-factory>
 
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/journaldev</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
 
        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Specify session context -->
        <property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
        <!-- Show SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- Referring Mapping File -->
        <mapping resource="domain-classes.hbm.xml"/>
    </session-factory>
 
</hibernate-configuration>
&#13;
&#13;
&#13;

域classes.hbm.xml:

&#13;
&#13;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.hibernate.data.Employee" table="employee">
        <id name="employeeId" column="EMP_ID" type="long">
            <generator class="native" />
        </id>
        <property name="employeeName" column="EMP_NAME" type="string"/>
        <property name="employeeHireDate" column="EMP_HIRE_DATE" type="date"/>
        <property name="employeeSalary" column="EMP_SALARY" type="double"/>
    </class>
</hibernate-mapping>
&#13;
&#13;
&#13;

Main.java:

&#13;
&#13;
package com.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.hibernate.data.Employee;

public class Main {
public static void main(String [] args){
    //Create a configuration instance
    Configuration configuration = new Configuration();
    //Provide configuration file
    configuration.configure("hibernate.cfg.xml");
    //Build a SessionFactory
    SessionFactory factory = configuration.buildSessionFactory(new  StandardServiceRegistryBuilder().configure().build());
    //Get current session, current session is already associated with  Thread
    Session session = factory.getCurrentSession();
    //Begin transaction, if you would like save your instance
    session.getTransaction().begin();
    //Create employee
    Employee emp = new Employee();
    emp.setEmployeeName("Peter Jousha");
    emp.setEmployeeSalary(2000);
    emp.setEmployeeHireDate(new Date());
    //Save
    session.save(emp);
    //Commit,calling of commit will cause save an instance of employee
    session.getTransaction().commit();
}
}
&#13;
&#13;
&#13;

在这个实现阶段,我应该看到实例自动添加到数据库中。但是我的数据库没有任何变化。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要调用session.flush()Employee实体写入数据库。试试这个:

public static void main(String [] args){
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    SessionFactory factory = configuration.buildSessionFactory(new  StandardServiceRegistryBuilder().configure().build());
    Session session = factory.getCurrentSession();
    Transaction tx = session.getTransaction();

    Employee emp = new Employee();
    emp.setEmployeeName("Peter Jousha");
    emp.setEmployeeSalary(2000);
    emp.setEmployeeHireDate(new Date());

    session.save(emp);
    session.flush();    // <-- forces Hibernate to write your Employee to the database

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