hibernate的配置。。。

来源:互联网 发布:软件无线电相关项目 编辑:程序博客网 时间:2024/06/15 16:28

hibernate.cfg.xml文件的配置:

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh?createDatabaseIfNotExist=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置是否显示Hibernate生成的SQL语句 -->
<property name="show_sql">true</property>
<!-- 配置自动根据映射文件更新数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 配置currentSession的上下文环境 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 配置禁止hibernate查找校验器  -->
<property name="javax.persistence.validation.mode">none</property>
<!-- 配置二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 配置实体映射文件(或实体类)用的是注解方式 -->
<mapping class="cn.com.xalead.hibernate.entity.User"/>
<mapping class="cn.com.xalead.hibernate.entity.Group"/>
</session-factory>

</hibernate-configuration>

下面是Group类和User类的代码:

package cn.com.xalead.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "T_GROUP")
public class Group {
private int id;
private String groupName;
private Set<User> users = new HashSet<User>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getGroupName() {
return groupName;
}


public void setGroupName(String groupName) {
this.groupName = groupName;
}


@OneToMany(mappedBy = "group")
public Set<User> getUsers() {
return users;
}


public void setUsers(Set<User> users) {
this.users = users;
}
}
package cn.com.xalead.hibernate.entity;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "T_USER")
public class User {
private int id;
private String username;
private String password;
private Group group;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


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;
}


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "groupId")
public Group getGroup() {
return group;
}


public void setGroup(Group group) {
this.group = group;
}
}
下面是HibernateUtil类的代码

package cn.com.xalead.hibernate.factory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static Configuration cfg = null;
private static SessionFactory factory = null;
static {
if (cfg == null)
cfg = new Configuration().configure();
if (factory == null) {
// 创建服务注册类(这个是4.1版本的,3版本不需要创建ServiceRegistry 对象)
ServiceRegistry registry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
factory = cfg.buildSessionFactory(registry);
}
}


public static SessionFactory getSessionFactory() {
return factory;
}


public static Session openSession() {
return factory.openSession();
}


public static Session getSession() {
return factory.getCurrentSession();
}


public static void closeSession(Session session) {
if (session != null)
session.close();
}
}

原创粉丝点击