终于要说ManyToMany了
场景:Product和Customer。
先看TestProduct.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
{
private String id;
private String name;
private float price;
private List<TestCustomer> customers = new ArrayList<TestCustomer>();
/**
* @return the id
*/
@Id
public String getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the price
*/
public float getPrice()
{
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price)
{
this.price = price;
}
/**
* @return the customers
*/
@ManyToMany
public List<TestCustomer> getCustomers()
{
return customers;
}
/**
* @param customers the customers to set
*/
public void setCustomers(List<TestCustomer> customers)
{
this.customers = customers;
}
}
注意这里的ManyToMany什么都没有写。
再看TestCustomer.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
{
private String id;
private String tel;
private List<TestProduct> products = new ArrayList<TestProduct>();
/**
* @return the id
*/
@Id
public String getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return the tel
*/
public String getTel()
{
return tel;
}
/**
* @param tel the tel to set
*/
public void setTel(String tel)
{
this.tel = tel;
}
/**
* @return the products
*/
@ManyToMany(mappedBy = "customers")
public List<TestProduct> getProducts()
{
return products;
}
/**
* @param products the products to set
*/
public void setProducts(List<TestProduct> products)
{
this.products = products;
}
}
这里的ManyToMany我写了mappedBy这个attribute。
然后看hib产生的sql:
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(255 char) not null,
tel varchar2(255 char),
primary key (id)
);
create table test_product (
id varchar2(255 char) not null,
price float not null,
name varchar2(255 char),
primary key (id)
);
create table test_product_customers (
products_id varchar2(255 char) not null,
customers_id varchar2(255 char) not null
);
ok! 非常好。hib终于在ManyToMany上没有犯白痴了。
上面强调了mappedBy这个属性。其实,在annotation 系列中。都有提到mappedBy这个东西。只是,我没有说到底是什么意思。其实很简单:这个东西就相当于xml配置中的inverse。写了mappedBy就代表这个方法的返回值是被维护方。可以看看
的最后2句话。
这里,hib的配置方面我想该告一段落了。应该有点其他的东西了。
分享到:
相关推荐
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
Hibernate 快速入门的工程包 包括用xml和annotation映射数据表
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 ...
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...
2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 ...
2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. ...
2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二...
Hibernate原理的模拟,重点讲思路,代码不全部实现,Hibernate的基本配置 Hibernate核心接口介绍,对象的三种状态,Hibernate关系映射:一对一、 一对多 、 多对多。 Hibernate查询(HQL语句的使用)。
10.2.2 在Java SE环境下使用 Hibernate JPA实现 370 10.2.3 在Java SE环境下使用 TopLink JPA实现 374 10.2.4 在Java SE环境下使用 EntityManager 377 10.2.5 使用orm.xml管理O/R映射 379 10.3 理解实体 382 10.3.1 ...
8. Hibernate Validator Specifics 8.1. Public API 8.2. Fail fast mode 8.3. Method validation 8.3.1. Defining method-level constraints 8.3.2. Evaluating method-level constraints 8.3.3. Retrieving method...
15.2.1 使用@RequestMapping映射请求 15.2.2 请求处理方法签名概述 15.2.3 处理方法签名详细说明 15.2.4 使用HttpMessageConverter 15.2.5 处理模型数据 15.3 处理方法的数据绑定 15.3.1 数据绑定流程剖析 15.3.2 ...
15.2.1 使用@RequestMapping映射请求 15.2.2 请求处理方法签名概述 15.2.3 处理方法签名详细说明 15.2.4 使用HttpMessageConverter 15.2.5 处理模型数据 15.3 处理方法的数据绑定 15.3.1 数据绑定流程剖析 15.3.2 ...
开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...
(读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看...