@ManyToMany- annotation关系映射篇(下)
来源:互联网 发布:女网络歌手2016 编辑:程序博客网 时间:2024/06/07 22:34
终于要说ManyToMany了
场景:Product和Customer。
先看TestProduct.java
Java代码
- package
net.paoding.forum.domain; -
- import
java.util.ArrayList; - import
java.util.List; -
- import
javax.persistence.Entity; - import
javax.persistence.Id; - import
javax.persistence.ManyToMany; -
- @Entity
- public
class TestProduct - {
-
String id; -
String name; -
float price; -
List<TestCustomer> newcustomers = ArrayList<TestCustomer>(); -
-
-
-
String getId() -
{ -
id; -
} -
-
-
void setId(String id) -
{ -
= id; -
} -
-
-
String getName() -
{ -
name; -
} -
-
-
void setName(String name) -
{ -
= name; -
} -
-
-
float getPrice() -
{ -
price; -
} -
-
-
void setPrice( floatprice) -
{ -
= price; -
} -
-
-
-
List<TestCustomer> getCustomers() -
{ -
customers; -
} -
-
-
void setCustomers(List<TestCustomer> customers) -
{ -
= customers; -
} -
- }
再看TestCustomer.java
Java代码
- package
net.paoding.forum.domain; -
- import
java.util.ArrayList; - import
java.util.List; -
- import
javax.persistence.Entity; - import
javax.persistence.Id; - import
javax.persistence.ManyToMany; -
- @Entity
- public
class TestCustomer - {
-
String id; -
String tel; -
List<TestProduct> newproducts = ArrayList<TestProduct>(); -
-
-
-
String getId() -
{ -
id; -
} -
-
-
void setId(String id) -
{ -
= id; -
} -
-
-
String getTel() -
{ -
tel; -
} -
-
-
void setTel(String tel) -
{ -
= tel; -
} -
-
-
= "customers") -
List<TestProduct> getProducts() -
{ -
products; -
} -
-
-
void setProducts(List<TestProduct> products) -
{ -
= products; -
} - }
这里的ManyToMany我写了mappedBy这个attribute。
然后看hib产生的sql:
Java代码
- drop
table test_customer cascade constraints; - drop
table test_product cascade constraints; - drop
table test_product_customers cascade constraints; -
- create
table test_customer ( -
id varchar2( char) not null, -
tel varchar2( char), -
primary key (id) - );
-
- create
table test_product ( -
id varchar2( char) not null, -
price not null, -
name varchar2( char), -
primary key (id) - );
-
- create
table test_product_customers ( -
products_id varchar2( char) not null, -
customers_id varchar2( char) not null - );
ok! 非常好。hib终于在ManyToMany上没有犯白痴了。
上面强调了mappedBy这个属性。其实,在annotation 系列中。都有提到mappedBy这个东西。只是,我没有说到底是什么意思。其实很简单:这个东西就相当于xml配置中的inverse。写了mappedBy就代表这个方法的返回值是被维护方
0 1
- @ManyToMany- annotation关系映射篇(下)
- @ManyToMany- annotation关系映射篇(下)
- @ManyToMany- annotation关系映射篇(下)
- Hibernate关系映射(六)多对多单向关联@ManyToMany Annotation方式
- Hibernate关系映射(七)多对多双向关联@ManyToMany Annotation方式
- Hibernate12_映射关系---manyTomany
- 如何利用Hibernate4的annotation配置多对多(ManyToMany)关系?
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Annotation实现关系映射
- hibernate--annotation关系映射
- @OneToMany或@ManyToOne的用法-annotation关系映射篇(上)
- 我看Openstack的临时(Ephemeral)存储和块(Block)存储
- 【2014.03.30】实习开始了
- java IO一般用到的类
- AS3.0中的ApplicationDomain
- @ManyToMany- annotation关系映射篇(下)
- 一位数据分析师的职业规划
- jstl学习笔记
- 基础加强-代理
- PostgreSQL学习手册(PL/pgSQL过程语言)
- android 数据库操作
- 读《相约星期二》
- Java并发编程之线程管理(Executor框架16)
- 表达式转换【PAT】