JPA 使用

本文以JPA+Hibernate 角色与权限示例说明。

角色实体定义:

@Entity
@Table
public class Role {
    private long id;
    private String name;
    private String type;
    private Timestamp createTime;
    private Set<Resource> resources=new HashSet<Resource>();
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

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

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @ManyToMany
    @JoinTable(name = "role_resource",joinColumns = {@JoinColumn(name="roleId", referencedColumnName = "id")},inverseJoinColumns = {@JoinColumn(name="resourceId", referencedColumnName = "id")})
    public Set<Resource> getResources() {
        return resources;
    }

    public void setResources(Set<Resource> resources) {
        this.resources = resources;
    }

资源实体定义

@Entity
@Table
public class Resource {
    private long id;
    private String name;
    private String title;
    private String description;
    private String icon;
    private StatisticsType type;
    private String url;
    private long orderNumber;
    private boolean first;
    private Timestamp createTime;
    private Set<Role> roles=new HashSet<Role>();

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

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

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Column
    public StatisticsType getType() {
        return type;
    }

    public void setType(StatisticsType type) {
        this.type = type;
    }

    @Column
    public String getUrl() {
        return url;
    }

    @Column
    public void setUrl(String url) {
        this.url = url;
    }

    public long getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(long orderNumber) {
        this.orderNumber = orderNumber;
    }

    @Column
    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    @Column
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @ManyToMany(mappedBy = "resources")
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

说明:角色和资源之间为多对多的关系,通过@ManyToMany注解表示。ManyToMany的属性mappedBy指明关系的维护方,哪个实体@ManyToMany指定mappedBy说明此实体为关系的被维护方。以上角色和资源中,角色即为关系维护方,资源为被维护方。

数据库配置:

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="maxActive" value="50" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="1" />
        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 'x' FROM DUAL" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <!-- mysql 不支持 poolPreparedStatements -->
        <!--<property name="poolPreparedStatements" value="true" /> -->
        <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20"
            /> -->
        <!-- 开启Druid的监控统计功能 -->
        <property name="filters" value="stat" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.vrvwh.wh01.domain</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=${dialect}
                hibernate.show_sql=${hibernate.show_sql}
                hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
                cache.provider_class=${hibernate.cache.provider_class}
                cache.use_second_level_cache=${hibernate.cache.use_second_level_cache}
                cache.use_query_cache=${hibernate.cache.use_query_cache}
                hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
            </value>
        </property>
    </bean>

 

posted @ 2015-02-25 17:33  tyb1222  阅读(605)  评论(0编辑  收藏  举报