使用enum 存储Permission的值
package demo;
public enum Permission {
CREATE,DELETE,UPDATE;
}
Role与Permission是多对多的关系:
package demo;
import java.util.Set;
public class Role {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private Set<Permission> permissions;
public Set<Permission> getPermissions() {
return permissions;
}
public void setPermissions(Set<Permission> permissions) {
this.permissions = permissions;
}
}
Role.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<typedef name="permission" class="demo.Permission">
<param name="enumClassName">demo.Permission</param>
</typedef>
<class name="demo.Role" entity-name="role" table="role">
<id name="id">
<generator class="native" />
</id>
<set name="permissions" table="permission">
<key column="rid" not-null="true" />
<element type="permission" column="permission" />
</set>
</class>
</hibernate-mapping>
运行后数据库生成数据表如下: