Hibernate註解配置N N關聯

2021-09-08 03:41:39 字數 1462 閱讀 9479

多對多

通過 @manytomany 註解定義多對多關係,同時通過 @jointable 註解描述關聯表和關聯條件。其中一端定義為 owner, 另一段定義為 inverse(對關聯表進行更新操作,這段被忽略)。

@entity

public class employer implements serializable

)@jointable(

name="employer_employee",

joincolumns=@joincolumn(name="emper_id"),

inversejoincolumns=@joincolumn(name="empee_id")

)public collection getemployees()

...}

@entity

public class employee implements serializable ,

targetentity = employer.class

)public collection getemployers()

}預設值:

關聯表名:主表表名 + 下劃線 + 從表表名;關聯表到主表的外來鍵:主表表名 + 下劃線 + 主表中主鍵列名;關聯表到從表的外鍵名:主表中用於關聯的屬性名 + 下劃線 + 從表的主鍵列名。

用 cascading 實現傳播持久化(transitive persistence)

cascade 屬性接受值為 cascadetype 陣列,其型別如下:

• cascadetype.persist: cascades the persist (create) operation to associated entities persist() is called or if the entity is managed 如果乙個實體是受管狀態,或者當 persist() 函式被呼叫時,觸發級聯建立(create)操作。

• cascadetype.merge: cascades the merge operation to associated entities if merge() is called or if the entity is managed 如果乙個實體是受管狀態,或者當 merge() 函式被呼叫時,觸發級聯合並(merge)操作。

• cascadetype.remove: cascades the remove operation to associated entities if delete() is called 當 delete() 函式被呼叫時,觸發級聯刪除(remove)操作。

• cascadetype.refresh: cascades the refresh operation to associated entities if refresh() is called  當 refresh() 函式被呼叫時,出發級聯更新(refresh)操作。

• cascadetype.all: all of the above  以上全部

Hibernate 使用註解配置對映關係

寫在前面 配置實體類與資料庫的對映關係,有兩種方式 1.使用 hbm.xml 2.使用 註解 一 註解的方式 1.entity 加在類的前面,將類宣告為持久化類。2.table 加在類的前面,為類宣告關聯的表名。如果表名和類名相同,可以省略。eg table name tbl role 3.prox...

hibernate 註解方式配置實體的對映

hibernate註解 1。entity name entityname 必須,name為可選,對應資料庫中一的個表 2 table name catalog schema 可選,通常和 entity配合使用,只能標註在實體的class定義處,表示實體對應的資料庫表的資訊 name 可選,表示表的名...

Hibernate註解配置1 N關聯

多對一 使用 manytoone 註解定義多對一關係。entity public class flight implements serializable joincolumn name comp id public company getcompany 其中 joincolumn 註解是可選的,關...