many-to-many
来源:互联网 发布:台湾文献数据库 编辑:程序博客网 时间:2024/05/06 04:21
//-----------------------------关联表---------------------------------@Entity@Table(name = "CATEGORY_ITEM")@org.hibernate.annotations.Immutablepublic class CategorizedItem {//使用符合主键 @Embeddable public static class Id implements Serializable { @Column(name = "CATEGORY_ID") protected Long categoryId; @Column(name = "ITEM_ID") protected Long itemId; public Id() { } public Id(Long categoryId, Long itemId) { this.categoryId = categoryId; this.itemId = itemId; } public boolean equals(Object o) { if (o != null && o instanceof Id) { Id that = (Id) o; return this.categoryId.equals(that.categoryId) && this.itemId.equals(that.itemId); } return false; } public int hashCode() { return categoryId.hashCode() + itemId.hashCode(); } @Column(updatable = false) @NotNull protected String addedBy; @Column(updatable = false) @NotNull protected Date addedOn = new Date(); F Maps timestamp //映射为OneToMany @ManyToOne @JoinColumn( name = "CATEGORY_ID",insertable = false, updatable = false) protected Category category; G Maps category @ManyToOne @JoinColumn( name = "ITEM_ID",insertable = false, updatable = false) protected Item item; H Maps item //值id,由程序生成 @EmbeddedId protected Id id = new Id();public CategorizedItem( String addedByUsername, Category category, Item item) { this.addedBy = addedByUsername; this.category = category; this.item = item; this.id.categoryId = category.getId(); this.id.itemId = item.getId(); category.getCategorizedItems().add(this); item.getCategorizedItems().add(this); }// ...}//-------------------------两个多对多实体-----------------------------------@Entitypublic class Category { //放弃管理 @OneToMany(mappedBy = "category") protected Set<CategorizedItem> categorizedItems = new HashSet<>(); // ...}@Entitypublic class Item {@OneToMany(mappedBy = "item")protected Set<CategorizedItem> categorizedItems = new HashSet<>();// ...}//-------------------------------持久化---------------------------------Category someCategory = new Category("Some Category");Category otherCategory = new Category("Other Category");em.persist(someCategory);em.persist(otherCategory);Item someItem = new Item("Some Item");Item otherItem = new Item("Other Item");em.persist(someItem);em.persist(otherItem);CategorizedItem linkOne = new CategorizedItem("johndoe", someCategory, someItem);CategorizedItem linkTwo = new CategorizedItem("johndoe", someCategory, otherItem);CategorizedItem linkThree = new CategorizedItem("johndoe", otherCategory, someItem);em.persist(linkOne);em.persist(linkTwo);em.persist(linkThree);
0 0
- many-to-many性能
- hibernate-----many-to-many
- Hibernate many to many
- hibernate many-to-many
- many to many
- Many-to-many Query
- Many-to-Many
- many-to-many
- Hibernate Many-to-Many
- Hibernate Many-To-Many Revisited
- hibernate many to many查询
- hibernate映射many-to-many
- grials many-to-many配置
- django:Many-to-many关系
- Hibernate Many-to-Many Mappings
- Many to many relationships&Assignment
- django:Many-to-many关系
- hibernate映射many-to-many
- ACM:蓝桥杯:日期计算(闰年判断方法)
- OneToMany/ManyToOne关联
- Android中EditText如何去除边框添加下划线
- Hibernate OneToOne关联
- 农村土地确权之调查公示 ——第二轮公示注意事项说明
- many-to-many
- Java练习:简单粗暴法选择排序
- SpringMVC之http请求头和响应头(四)
- jpa 脚本生成数据库以及数据完整
- sizeof(struct )大小讨论
- HDU 1394 Minimum Inversion Number (离散化 + 树状数组 求逆序对)
- JPA持久化上下文与对象状态
- web服务器集群(多台web服务器)session同步、共享的3种解决方法
- shell简单编程