JPA 系列教程20-JPA2.0-@CollectionTable

@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

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

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值