Hibernate4实体映射自动生成数据库表结构

实体映射关系建立之后,不需要再花时间到数据库去建立数据表结构。可以由Hibernate直接生成后台。



Userinfo.java

package org.kj.hibernate.bean;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "userinfo", schema = "dbo", catalog = "MySQL")
public class Userinfo {

	private int id;
	private String uname;
	private String cellphone;
	private String password;
	private List<Role> role;
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name ="uname")
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	
	@Column(name ="password")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "user")
	public List<Role> getRole() {
		return role;
	}
	public void setRole(List<Role> role) {
		this.role = role;
	}
	
	@Column(name ="cellphone")
	public String getCellphone() {
		return cellphone;
	}
	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}
	
	
}

Role.java

package org.kj.hibernate.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "role", schema = "dbo", catalog = "MySQL")
public class Role {

	private int id;
	private int rid;
	private String rname;
	private Userinfo user;
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name ="rid")
	public int getRid() {
		return rid;
	}
	public void setRid(int rid) {
		this.rid = rid;
	}
	
	@Column(name ="rname")
	public String getRname() {
		return rname;
	}
	public void setRname(String rname) {
		this.rname = rname;
	}
	
	@ManyToOne(cascade = {CascadeType.ALL})
	@JoinColumn(name = "uid")
	public Userinfo getUser() {
		return user;
	}
	public void setUser(Userinfo user) {
		this.user = user;
	}
	
	
}

配置文件:hibernate.cfg.xml

<?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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">Admin123</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="myeclipse.connection.profile">sqlserver://localhost//MySQL</property>
    
    	<property name="show_sql">true</property>
    	<property name="hbm2ddl.auto">update</property>
    	<!-- <property name="hbm2ddl.auto">create</property> -->
		<!-- <property name="hibernate.hbm2ddl.auto">validate</property> -->
		
		<property name="hibernate.current_session_context_class">thread</property>
		<property name="javax.persistence.validation.mode">none</property> 
		
		
		
		 <!-- 映射文件引入 -->
		 <mapping class="org.kj.hibernate.bean.Userinfo" />
		 <mapping class="org.kj.hibernate.bean.Role" />
		 
		  
    </session-factory>

</hibernate-configuration>

Test文件:ClassToDB.java

package org.kj.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ClassToDB {
	public static void main(String[] args) {
		Configuration config = null;
		Session session = null;
		Transaction tx = null;
		SessionFactory sessionFactory;
		try {
			config = new Configuration().configure();// 默认找文件hibernate.hbm.xml
			
			//Hibernate4.0新增接口
			//sessionFactory = config.buildSessionFactory();
			ServiceRegistry serviceRegistry =  new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
			sessionFactory = config.buildSessionFactory(serviceRegistry);
            //session = sessionFactory.openSession();       

            /*System.out.println("Creating tables...");

			session = sessionFactory.openSession();
			tx = session.beginTransaction();

			SchemaExport schemaExport = new SchemaExport(config);
			schemaExport.create(true, true);

			System.out.println("Table created.");

			tx.commit();*/

		} catch (HibernateException e) {
			e.printStackTrace();
			try {
				tx.rollback();
			} catch (HibernateException e1) {
				e1.printStackTrace();
			}
		} finally {

		}
	}
}


你可能感兴趣的:(Hibernate,生成数据表结构,映射实体表)