如何更改此配置以便我可以使用Hibernate?

时间:2015-08-06 09:00:10

标签: java hibernate jpa

这是我的项目结构:

enter image description here

我有以下这些文件:

context.xml中

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/sakila"
              url="jdbc:mysql://localhost/sakila"
              username="root"
              password=""
              driverClassName="com.mysql.jdbc.Driver"
              type="javax.sql.DataSource"
              maxActive="20" maxIdle="5" maxWait="10000"
            />
</Context>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="sakila" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>java:comp/env/jdbc/sakila</non-jta-data-source>
    </persistence-unit>

</persistence>

在DAO课程中我可以使用:

EntityManagerFactory sakilaFactory = Persistence.createEntityManagerFactory("sakila");
EntityManager entityManager = sakilaFactory.createEntityManager();

一切正常..

但我想将配置更改为使用.xml文件而不是 EntityManager 我想使用Hibernate API中的 Session

我已经在网上看过几个教程,但我只是更加困惑。我应该如何使用.xml文件进行Hibernate配置?

我在Tomcat上运行应用程序,这是 pom.xml

的依赖项
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.10.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.10.Final</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
    <scope>provided</scope>
</dependency>

我想要实现的是正确的Hibernate配置,并使用Session而不是EntityManager。

编辑#1:这是我的Actor类的样子:

package biz.tugay.model;
/* User: koray@tugay.biz Date: 06/08/15 Time: 09:17 */

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "actor")
public class Actor {

    private Integer actor_id;
    private String first_name;
    private String last_name;
    private Timestamp last_update;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getActor_id() {
        return actor_id;
    }

    public void setActor_id(Integer id) {
        this.actor_id = id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public Timestamp getLast_update() {
        return last_update;
    }

    public void setLast_update(Timestamp last_update) {
        this.last_update = last_update;
    }

    @Override
    @Transient
    public String toString() {
        return "Actor{" +
                "id=" + actor_id +
                ", first_name='" + first_name + '\'' +
                ", last_name='" + last_name + '\'' +
                ", last_update=" + last_update +
                '}';
    }

}

1 个答案:

答案 0 :(得分:0)

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:PORT/databasename</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">XXXX</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create </property>
    <property name="hbm2ddl.auto">validate</property>


     <mapping class="com.packageClass"/>
      <mapping class="com.package.OtherClass"/>
</session-factory>

你应该有hibernate的hibernate.cfg.xml - 配置文件;我给了你一个上面的模板。它应该放在你的java资源的src中。现在,您可以使用注释来表示持久性。您还应该在actor类的每个属性上添加@Column注释,以映射表中属性的每个属性。这是一个示例类

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="useri")
public class User implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id_user;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="rol")
private String rol;

public Integer getId_user() {
    return id_user;
}

public void setId_user(Integer id_user) {
    this.id_user = id_user;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getRol() {
    return rol;
}

public void setRol(String rol) {
    this.rol = rol;
}

public User() {

}

}