JPA 系列教程14-自定义类型-@Embedded+@Embeddable

自定义类型

在hibernate中实现自定义类型,需要去实现UserType接口即可或者以Component的形式提供。

JPA的@Embedded注解有点类似,通过此注解可以在Entity模型中使用一般的java对象,不过此对象还需要用@Embeddable注解标注。

需求产生

Employee类有一个address属性,
address应该有city,street两个属性,
一般的写法直接在Employee类中写两个属性:
private String city;
private String street;

现在可以用一个Address类来代替此类写法,Address类包含了city和street,如此一来,我们在Employee类只要这样写:
private Address address;

ddl语句

CREATE TABLE `t_employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `city` varchar(255) DEFAULT NULL,
  `street` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

Address

package com.jege.jpa.embeddable;

import javax.persistence.Embeddable;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:实现自定义类型,在hibernate里面需要实现UserType接口
 */
@Embeddable
public class Address {
  private String city;
  private String street;

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getStreet() {
    return street;
  }

  public void setStreet(String street) {
    this.street = street;
  }

}

Employee

package com.jege.jpa.embeddable;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:@Embedded注解
 */
@Entity
@Table(name = "t_employee")
public class Employee {
  @Id
  @GeneratedValue
  private Long id;
  private String name;
  @Embedded
  private Address address;

  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 Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

}

MainTest

package com.jege.jpa.embeddable;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:自定义类型-@Embedded+@Embeddable测试
 */
public class MainTest {
  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() {
    Address address = new Address();
    address.setCity("beijing");
    address.setStreet("王府井大街");

    Employee employee = new Employee();
    employee.setAddress(address);
    employee.setName("jege");

    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
  }

  @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

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
微信打赏
支付宝打赏

原文地址:https://www.cnblogs.com/je-ge/p/6204530.html