JPA 使用

本文以JPA+Hibernate 角色與權限示例說明。mysql

角色實體定義:spring

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

資源實體定義sql

@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說明此實體爲關係的被維護方。以上角色和資源中,角色即爲關係維護方,資源爲被維護方。數據庫

數據庫配置:session

    <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>
相關文章
相關標籤/搜索