@CollectionTable

指定集合表的详细信息,如果是JPA1.0必须再写一个Pojo类

ddl语句

CREATE TABLE `t_employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; CREATE TABLE `t_colors` (
`employee_id` bigint(20) NOT NULL,
`color` varchar(255) DEFAULT NULL,
KEY `FK_105ywqy149ra7e238gil22nvr` (`employee_id`),
CONSTRAINT `FK_105ywqy149ra7e238gil22nvr` FOREIGN KEY (`employee_id`) REFERENCES `t_employee` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Employee

package com.jege.jpa;

import java.util.ArrayList;
import java.util.List; import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table; /**
* @author JE哥
* @email 1272434821@qq.com
* @description:pojo模型
*/
@Entity
@Table(name = "t_employee")
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
@ElementCollection
// 一对多集合,如果是JPA1.0必须再写一个Pojo类
@CollectionTable(name = "t_colors", joinColumns = @JoinColumn(name = "employee_id"))
@Column(name = "color")
private List<String> colors = new ArrayList<String>(); public Employee() { } public Employee(String name) {
this.name = name;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<String> getColors() {
return colors;
} public void setColors(List<String> colors) {
this.colors = colors;
} }

JPA2Test

package com.jege.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class JPA2Test { private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null; @BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
} @Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();
} @Test
public void persist() throws Exception {
Employee employee = new Employee();
employee.setName("je-ge"); employee.getColors().add("red");
employee.getColors().add("green");
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
} @Test
public void find() throws Exception {
persist();
entityManager.clear(); Employee employee = entityManager.find(Employee.class, 1L);
System.out.println(employee.getName());
System.out.println(employee.getColors()); } @Test
public void query() throws Exception {
persist();
Query query = entityManager.createQuery("select count(o) from Employee o");
Long count = (Long) query.getSingleResult();
System.out.println(count);
} // public Employee(String name) {
// this.name = name;
// }
@Test
public void query1() throws Exception {
persist();
Query query = entityManager.createQuery("select new Employee(o.name) from Employee o");
List<Employee> employees = query.getResultList();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
} @Test
public void query2() throws Exception {
persist();
Query query = entityManager.createQuery("select o from Employee o where o.name=?1").setParameter(1, "张三");
List<Employee> employees = query.getResultList();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
} @Test
public void query3() throws Exception {
persist();
Query query = entityManager.createQuery("select o from Employee o where o.name=:name").setParameter("name", "张三");
List<Employee> employees = query.getResultList();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
} @Test
public void query4() throws Exception {
persist();
Query query = entityManager.createNamedQuery("getAll");
List<Employee> employees = query.getResultList();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
} @Test
public void query5() throws Exception {
persist();
Query query = entityManager.createNativeQuery("select * from emp", Employee.class);
List<Employee> employees = query.getResultList();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
} @After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
} @AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
} }

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!



JPA 系列教程20-JPA2.0-@CollectionTable的更多相关文章

  1. JPA 系列教程21-JPA2.0-@MapKeyColumn

    @MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...

  2. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...

  3. JPA 系列教程16-继承-联合子类-JOINED

    联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...

  4. JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...

  5. JPA 系列教程12-复合主键-2个@Id+@IdClass

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...

  6. JPA 系列教程10-双向一对一关联表

    双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...

  7. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  8. JPA 系列教程5-双向一对多

    双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...

  9. JPA 系列教程4-单向一对多

    JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...

  10. JPA 系列教程3-单向多对一

    JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...

随机推荐

  1. 调试PostSharp DEMO 遇到的问题

    我今天在WIN7下的VS2010.VS2012调试PostSharp例子程序,下载的是PostSharp1.5版本,但是就是不进断点啊,我想了想这是怎么回事啊,于是我更换了2.0的再进行调试具然好使了 ...

  2. hdu 4198 Quick out of the Harbour(BFS+优先队列)

    题目链接:hdu4198 题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1. 需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次....... ...

  3. Hiberbate中的一对多关联查询

    Hibernate中一对多关系的应用 案例:一个人可以拥有多辆小轿车 目的:通过hibernate的相关配置,利用HQL语句成功的查询出某人拥有某些车辆 1. 项目结构 2. domain类的创建详情 ...

  4. Spring MVC---数据绑定和表单标签

                                   数据绑定和表单标签 数据绑定 数据绑定是将用户输入绑定到领域模型的一种特性,在Spring MVC的controller和view数据传递 ...

  5. MySQL5.7解压版详细安装教程,在最后一步需要随机密码

    这里为百度经验 http://jingyan.baidu.com/article/ff42efa93580c4c19e2202b6.html 然而在最后一步,回车不能够越过密码. 需要在解压的mysq ...

  6. Java 编译打包命令

    背景 编译 打包 解压 运行 参考 背景 我们有的时候总是要使用将自己写的工程编译成 class 文件,同时打包成 jar,虽然有各种工具可以帮助我们,但是毕竟掌握使用 java 本来的命令去做这些更 ...

  7. java基础练习 10

    import java.util.Scanner; public class Tenth { /*有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数*/ public stati ...

  8. 有关linux标准输出、标准输入、标准错误的重定向问题

    1.简单的命令行重定向问题. 例:ls  -al  test   test1  test2    1>out.txt  2>err.tx 这里ls这句命令行命令之后将标准输入重定向到out ...

  9. (四)python自带解释器(LDIE)的使用

    什么是IDE? Integrated Development Environment(集成开发环境) 打个不恰当的比方,如果说写代码是制作一件工艺品,那IDE就是机床.再打个不恰当的比方,PS就是图片 ...

  10. CSS控制文本在一行内显示,若有多余字符则使用省略号表示

    强制文本在一行内显示,多余字符使用省略号 text-overflow: ellipsis; overflow: hidden; white-space: nowrap;