hibernate 自动生成数据表

1、实体类

package cn.ecut.nsfw.role.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户角色
 * @author pengYi
 *
 */
public class Role implements Serializable {
	
	private String roleId;
	private String roleName;
	private String roleStatus;
	
	//一个用户对应多个权限
	private Set<RolePrivilege> roleSet = new HashSet<RolePrivilege>();
	
	public Set<RolePrivilege> getRoleSet() {
		return roleSet;
	}
	public void setRoleSet(Set<RolePrivilege> roleSet) {
		this.roleSet = roleSet;
	}
	//角色状态
	public static String ROLE_STATUS_VALID = "1";//有效
	public static String ROLE_STATUS_INVALID = "0";//无效
	
	public String getRoleId() {
		return roleId;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getRoleStatus() {
		return roleStatus;
	}
	public void setRoleStatus(String roleStatus) {
		this.roleStatus = roleStatus;
	}
}

  2、配置文件 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name = "cn.ecut.nsfw.role.entity.Role" table="role">
        <id name="roleId" type="java.lang.String">
            <column name="roleId" length="32" />
            <generator class="uuid.hex" />
        </id>

        <property name="roleName" type="java.lang.String">
            <column name="roleName" length="20" not-null="true" />    
        </property>
        <property name="roleStatus" type="java.lang.String">
            <column name="roleStatus" length="20" not-null="true" />    
        </property>
        
        <!-- 一对多关系  -->
        <set name="roleSet">
            <key column="roleId"/>
            <one-to-many class = "cn.ecut.nsfw.role.entity.RolePrivilege"/>
        </set>
    
    </class>
</hibernate-mapping>
    

注意事项:一定要说明属性类型如上,java.lang.String   以及 在表中的列的名称、长度、not-null 等属性

3、结果

 

posted @ 2017-06-19 16:44  苦海无涯、苦尽甘来  阅读(217)  评论(0编辑  收藏  举报