hibernate表與表之間的關(guān)系

1、表與表之間的關(guān)系

  • 一對(duì)一 (默認(rèn)主鍵關(guān)聯(lián))
    • 首先在實(shí)體中描述實(shí)體與實(shí)體之間的關(guān)系,然后在映射文件中描述兩個(gè)實(shí)體之間的關(guān)系 <one-to-one>
    • name代表另外一個(gè)在實(shí)體在實(shí)體中的屬性名
    • class :代表另外一個(gè)類的全路徑名
    <class name="com.hemi.bean2.User"  table="user">
    <id name="id"  column="id">
    <generator class="native"></generator>
    </id>
    <property name="name" column="name"></property>
    <property name="age" column="age"></property>
    <one-to-one name="sex" class="com.hemi.bean2.Sex"></one-to-one>
</class>
  • 一對(duì)一 (外鍵關(guān)聯(lián))注意:要把看做特殊的多對(duì)一
    • 首先在實(shí)體中描述實(shí)體與實(shí)體之間的關(guān)系一張表 在外鍵被約束一方(即添加外鍵的那個(gè))使用<many-to-one name="代表另外一個(gè)在實(shí)體在實(shí)體中的屬性名" unique="true" /> 唯一外鍵約束,才能準(zhǔn)確描述一對(duì)一關(guān)系
    • 另一張表 使用<one-to-one name="代表另外一個(gè)在實(shí)體在實(shí)體中的屬性名" property-ref="外鍵名" />代表依賴外鍵來(lái)約束關(guān)系
<class name="com.hemi.bean2.Sex"  table="sex">
    <id name="id"  column="id">
    <generator class="native"></generator>
    </id>
    <property name="sex" column="sex"></property>
    <one-to-one name="user" property-ref="sid"></one-to-one>
</class>

有外鍵約束的一方

<class name="com.hemi.bean2.User"  table="user">
    <id name="id"  column="id">
    <generator class="native"></generator>
    </id>
    <property name="name" column="name"></property>
    <property name="age" column="age"></property>
<many-to-one name="sex" column="sid" unique="true"></many-to-one>
</class>

  • 一對(duì)多
    首先在實(shí)體中描述實(shí)體與實(shí)體之間的關(guān)系
    然后在映射文件中描述兩個(gè)實(shí)體之間的關(guān)系
    <set name="實(shí)體集合屬性名">
    <key column="外鍵名"/>
    <one-to-many class="多方類的全路徑名"/>
    </set>
<class name="com.hemi.bean.District"  table="district">
    <id name="id"  column="id">
    <generator class="native"></generator>
    </id>
    <property name="name" column="name"></property>
    <set name="streets" cascade="all " inverse="true">
    <key column="District_id"></key>
    <one-to-many class="com.hemi.bean.Street" />
    </set>
</class>
  • 多對(duì)一
    首先在實(shí)體中描述實(shí)體與實(shí)體之間的關(guān)系
    然后在映射文件中描述兩個(gè)實(shí)體之間的關(guān)系
    <many-to-one name="一方類在該類中的屬性名" column="外鍵名"/>
<class name="com.hemi.bean.Street"  table="street">
    <id name="id"  column="id">
    <generator class="native"></generator>
    </id>
    <property name="name" column="name"></property> 
    <many-to-one name="district" column="District_id" ></many-to-one>
</class>

2、操作屬性 :

  • 級(jí)聯(lián)操作
    cascade:負(fù)責(zé)維護(hù)表與表之間的增刪改操作 ,屬性值:none(默認(rèn))、save-update、delete、all、merge
private static void save() {
        Session session = HibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();
        //創(chuàng)建街道對(duì)象
        Street street = new Street();
        street.setName("倉(cāng)前街道1");
        //創(chuàng)建區(qū)對(duì)象
        District district = new District();
        district.setName("余杭區(qū)1");
        //創(chuàng)建set集合將街道添加進(jìn)去
        HashSet<Street> set = new HashSet<Street>();
        set.add(street);
        //設(shè)置區(qū)對(duì)象的街道集合 
        district.setStreets(set);
        //通過保存區(qū)    同時(shí)會(huì)在街道表中添加數(shù)據(jù)
        session.save(district);
        transaction.commit();
        session.close();
    }
  • 關(guān)系維護(hù)

    inverse:負(fù)責(zé)維護(hù)表與表之間的關(guān)系,默認(rèn)為:false,代表雙方維護(hù)表的關(guān)系, true:代表放棄表的維護(hù) (一對(duì)多中)只有一方才有權(quán)利放棄 ,多方必須維護(hù),所以沒有該屬性

3、延遲加載 lazy屬性 默認(rèn)值為true(延遲加載)

類級(jí)別 :在class標(biāo)簽內(nèi)加屬性lazy 默認(rèn)true 對(duì)load方法起作用 java代碼取的時(shí)候get(都是立即加載) load(根據(jù)lazy屬性 true延遲加載 false立即加載)
關(guān)聯(lián)級(jí)別:

  • 一對(duì)多:
    • 一方在set標(biāo)簽內(nèi)加lazy屬性默認(rèn)true,extra極其懶惰
    • 多方在many-to-one標(biāo)簽內(nèi)加屬性lazy默認(rèn)false,proxy 懶加載 no-proxy無(wú)代理懶加載

4、hql連接查詢

(注意:此處的內(nèi)連接是標(biāo)準(zhǔn)內(nèi)連接: inner join,連表查詢時(shí)不能同時(shí)用兩張表的類 必須通過該類的屬性點(diǎn)出)
內(nèi)連接 :返回的是對(duì)象數(shù)組
迫切內(nèi)連接 :返回的是封裝對(duì)象
外連接:返回的是對(duì)象數(shù)組
迫切外連接:返回的是封裝對(duì)象

        Session session = HibernateUtil.getSession();
        //標(biāo)準(zhǔn)內(nèi)連接
        String hql="from District d inner join  d.streets";
        //迫切內(nèi)連接
        String hql1="from District d inner join  fetch d.streets";
        //方言內(nèi)連接-------返回的是對(duì)象數(shù)組
        String hql2="select s.name,d.name from Street s,District d where s.district.id=d.id";
        //方言內(nèi)連接-------返回的是對(duì)象
        String hql3="select new com.hemi.bean.Address(d.name,s.name) from Street s,District d where s.district.id=d.id";
        Query query = session.createQuery(hql1);
//      List<Address> list = query.list();
//      for (Address address : list) {
//          System.out.println(address);
//      }
        
//      List<Object[]> list = query.list();
//      for (Object[] objects : list) {
//          System.out.println(Arrays.toString(objects));
//      }
        List list = query.list();
        System.out.println(list);
        session.close();

通過注解描述類與表的關(guān)系 簡(jiǎn)單介紹

@Entity 注解將一個(gè)類聲明為實(shí)體Bean @Table對(duì)應(yīng)的數(shù)據(jù)庫(kù)中的表

@Id 主鍵
@GeneratedValue設(shè)置主鍵生成策略 GenerationType.IDENTITY 自動(dòng)增長(zhǎng)
@Column(name="id") 設(shè)置數(shù)據(jù)庫(kù)表中對(duì)應(yīng)的字段如果與實(shí)體類中屬性名相同 則不需要設(shè)置里面的name屬性


@Entity
@Table(name="student")
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private int age;

@GenericGenerator用來(lái)自定義主鍵生成策略

@Entity
@Table(name="animals")
public class Animal {
    @Id
    @GenericGenerator(name="uu",strategy="uuid") @GeneratedValue(generator="uu")
    @Column
    private String id;
    @Column
    private String name;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容