hibernate實現JPA規範配置

來源:互聯網
上載者:User

標籤:target   編號   資料   一對多   -o   欄位名   層級   null   string   

JAP(java Persistence API)sun公司推出一套基於ORM的規範
hibernate實現了這套規範
hibernate有自己獨立的ORM操作資料庫方式,也有JPA規範實現的操作資料庫方式
jar包是:hibernate-entitymanager-5.0.7.Final.jar
在src下建立META-INF檔案夾下建立一個名稱為persistence.xml的檔案




persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <!--在根標籤下至少要存在一個持久化單元 (有一個資料庫的串連資訊)-->
    <persistence-unit name="aaa">
            <properties>
                <!-- 配置多條資料庫的資訊 -->
                    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
                    <property name="hibernate.connection.url" value="jdbc:mysql:///jpa"></property>
                    <property name="hibernate.connection.username" value="root"></property>
                    <property name="hibernate.connection.password" value="1234"></property>
                    <!-- 方言 -->
                    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
                    
                    <property name="hibernate.show_sql" value="true"></property>
                    <property name="hibernate.format_sql" value="true"></property>
                    <property name="hibernate.hbm2ddl.auto" value="update"></property>
                    
                    <!-- 配置c3p0 -->
                    <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"></property>
            </properties>
    </persistence-unit>
</persistence>
約束的聲明可在hibernate-entitymanager-5.0.7.Final.jar--org.hibernate.jpa--persistence_2_0.xsd(中第24行到26行)




(一對多的關係配置案例:)
Customer:
        // ps: 所有jpa的註解都在 javax.persistence包下
        @Entity
        @Table(name="cst_customer")
        public  class Customer
        {
             @Id
             @Column(name="cust_id")
             @GeneratedValue(strategy=GenerationType.IDENTITY)
             private Long cust_id; // ‘客戶編碼(主鍵)‘,   0  null
            
             @Column(name="cust_name")
             private String cust_name; // ‘客戶名稱(公司名稱)‘,
            
             @Column(name="cust_source")
             private String cust_source; // ‘客戶資訊來源‘,
            
             @Column(name="cust_industry")
             private String cust_industry; // ‘客戶所屬行業‘,
            
             @Column(name="cust_level")
             private String cust_level; //‘客戶層級‘,
            
             @Column(name="cust_address")
             private String cust_address; // ‘客戶聯絡地址‘,
            
             @Column(name="cust_phone")
             private String cust_phone; // ‘客戶聯絡電話‘,
            
             // 有連絡人的集合
            /* targetEntity:對方的類型
             * mappedBy: 自己在對方中的屬性名稱 (ps:mappedBy出現在哪一方,哪一方意味著要放棄外鍵維護)
             * */
             @OneToMany(targetEntity=LinkMan.class,mappedBy="customer",cascade=CascadeType.ALL)
             private Set<LinkMan> linkmans=new HashSet<LinkMan>();

LinkMan:
        @Entity
        @Table(name="cst_linkman")
        public class LinkMan
        {
              @Id
              @Column(name="lkm_id")
              @GeneratedValue(strategy=GenerationType.IDENTITY)
              private Long lkm_id;// ‘連絡人編號(主鍵)‘,
              
              @Column(name="lkm_name")
              private String lkm_name;// ‘連絡人姓名‘,
              
              @Column(name="lkm_gender")
              private String lkm_gender;// ‘連絡人性別‘,
              
              @Column(name="lkm_phone")
              private String lkm_phone;// ‘連絡人辦公電話‘,
              
              @Column(name="lkm_mobile")
              private String lkm_mobile;// ‘連絡人手機‘,
              
              @Column(name="lkm_email")
              private String lkm_email;// ‘連絡人郵箱‘,
              
              @Column(name="lkm_qq")
              private String lkm_qq;//‘連絡人qq‘,
              
              @Column(name="lkm_position")
              private String lkm_position;// ‘連絡人職位‘,
              
              @Column(name="lkm_memo")
              private String lkm_memo;// ‘連絡人備忘‘,
              
              // 在多的一方有一的一方的對象--外鍵
              @ManyToOne(targetEntity=Customer.class,cascade=CascadeType.ALL)
             /* name:外鍵的欄位名
              referencedColumnName:指向的主鍵欄位名*/
              @JoinColumn(name="lkm_cust_id",referencedColumnName="cust_id")
              private Customer customer;

              
              
              
              
              
(多對多的關係案例:)
Role:
        @Entity
        @Table(name="sys_role")
        public class Role
        {
             @Id
             @Column(name="role_id")
             @GeneratedValue(strategy=GenerationType.IDENTITY)
             private Long role_id;// id
            
             @Column(name="role_name")
             private String role_name;//‘角色名稱‘,
            
             @Column(name="role_memo")
             private String role_memo;// ‘備忘‘,
            
             // 有使用者的集合
             /*
              * targetEntity:對方的類型
              * mappedBy:自己在對方中的屬性名稱
              * */
             @ManyToMany(targetEntity=User.class,mappedBy="roles",cascade=CascadeType.PERSIST)
             private Set<User> users=new HashSet<User>();
            
User:
        @Entity
        @Table(name="sys_user")
        public class User
        {
            @Id
            @Column(name="user_id")
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            private Long user_id;// ‘使用者id‘,
            
            @Column(name="user_code")
            private String user_code;//‘使用者帳號‘,
            
            @Column(name="user_name")
            private String user_name;// ‘使用者名稱稱‘,
            
            @Column(name="user_password")
            private String user_password;// ‘使用者密碼‘,
            
            @Column(name="user_state")
            private String user_state;// ‘1:正常,0:暫停‘,
            
            // 有角色的集合
            /*targetEntity:對方的類型
             *
             * */
            @ManyToMany(targetEntity=Role.class,cascade=CascadeType.ALL)
            /*name: 中間表的名稱
            joinColumns:自己在中間的一些配置
            inverseJoinColumns:對方在中間表的一些配置*/
            @JoinTable(name="sys_user_role",
            joinColumns={
                    /*name:自己在中間表的欄位名
                    referencedColumnName:指向自己主鍵的欄位名*/
                @JoinColumn(name="user_id",referencedColumnName="user_id")            
            },
            inverseJoinColumns={
                    /*name:對方在中間表的欄位名
                    referencedColumnName:指向對方主鍵的欄位名*/
                    @JoinColumn(name="role_id",referencedColumnName="role_id")
            })
             private Set<Role> roles=new HashSet<Role>();
           

 

 

 

1.1 JAP和hibernate中操作資料的方法對照

操作

Hibernate中的方法

JPA中的方法

說明

儲存操作

save(Object entity)

persist(Object entity)

共同點:都是把臨時態對象轉成了持久態。

區別:

提供者不一樣:

       save方法是hibernate提供的。

       persist方法是JPA規範提供的。

在沒有事務的情況下:

       save會去資料庫中儲存,hibernate提供了一個內建的事務來執行。

       persist什麼都不會做。

更新操作

update (Object entity)

merge (Object entity)

Hibernate和jpa都可以利用快照機制,不調用任何方法去更新。

Update方法在更新時,如果遇到一級緩衝已經包含了一個相同OID的對象會報錯。merge則可以執行成功。

刪除操作

delete (Object entity)

remove (Object entity)

都是刪除一個實體

查詢一個操作

get (Class clazz,Serializable id)

load(Class clazz,Serializable id)

find(Class clazz,Object id)

getReerence(Class clazz,Object id)

get和find都是立即載入。load和getReference一樣都是消極式載入。

查詢所有操作

Query:使用HQL語句查詢

Query:使用JPQL查詢

查詢語句的形式不一樣。

查詢返回唯一結果操作

uniqueResult()

getSingleResult()

查詢都是返回一個唯一的結果。

 

 

hibernate實現JPA規範配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.