日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

JPA 使用

發(fā)布時(shí)間:2024/4/24 综合教程 33 生活家
生活随笔 收集整理的這篇文章主要介紹了 JPA 使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文以JPA+Hibernate 角色與權(quán)限示例說(shuō)明。

角色實(shí)體定義:

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

資源實(shí)體定義

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

說(shuō)明:角色和資源之間為多對(duì)多的關(guān)系,通過(guò)@ManyToMany注解表示。ManyToMany的屬性mappedBy指明關(guān)系的維護(hù)方,哪個(gè)實(shí)體@ManyToMany指定mappedBy說(shuō)明此實(shí)體為關(guān)系的被維護(hù)方。以上角色和資源中,角色即為關(guān)系維護(hù)方,資源為被維護(hù)方。

數(shù)據(jù)庫(kù)配置:

    <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"
            /> -->
        <!-- 開(kāi)啟Druid的監(jiān)控統(tǒng)計(jì)功能 -->
        <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>

總結(jié)

以上是生活随笔為你收集整理的JPA 使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。