JPA 菜鸟教程

JPA 菜鸟教程 18 自动把firstName+lastNam

2017-02-11  本文已影响35人  JeGe

需求

设计的国际化网站,页面需要输入firstName,lastName,后台数据库只需要存储name属性。
页面获取的firstName,lastName持久化到数据库name属性,规则按照,分隔保存。
防止错误修改,模型里面的name属性不提供getter,setter方法。

ddl语句

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

User

package com.jege.jpa.primary;

import java.util.Date;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:自动把firstName+lastName合并为name
 */
@Entity
@Table(name = "t_user")
@Access(AccessType.FIELD)
public class User {
  @Id
  @GeneratedValue
  private Long id;
  @Column(name = "name", unique = true)
  private String name;
  private Date birthday;
  @Transient
  private String firstName;
  @Transient
  private String lastName;

  @PostLoad
  private void load() {
    if (name != null) {
      String[] names = name.split(",");
      firstName = names[0];
      lastName = names[1];
    }
  }

  @PrePersist
  @PreUpdate
  private void save() {
    if (firstName != null && !"".equals(firstName)) {
      name = firstName + ",";
    }
    if (lastName != null && !"".equals(lastName)) {
      name += lastName;
    }
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Date getBirthday() {
    return birthday;
  }

  public void setBirthday(Date birthday) {
    this.birthday = birthday;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", firstName=" + firstName + ", lastName="
    + lastName + "]";
  }

}

MainTest

package com.jege.jpa.primary;

import java.util.Date;

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哥
 * @entityManagerail 1272434821@qq.com
 * @description:自动把firstName+lastName合并为name测试
 */
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() throws Exception {
    User user = new User();
    user.setBirthday(new Date());
    user.setFirstName("西门");
    user.setLastName("吹雪");

    entityManager.getTransaction().begin();
    entityManager.persist(user);
    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

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

微信打赏微信打赏
支付宝打赏支付宝打赏
上一篇下一篇

猜你喜欢

热点阅读