JPA 系列教程18-自动把firstName+lastName合并为name字段
需求
设计的国际化网站,页面需要输入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();
}
}
}
源码地址
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程18-自动把firstName+lastName合并为name字段的更多相关文章
- JPA 系列教程2-单表操作
JPA Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibe ...
- JPA 系列教程10-双向一对一关联表
双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...
- JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- JPA 系列教程3-单向多对一
JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...
- JPA 系列教程21-JPA2.0-@MapKeyColumn
@MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...
- JPA 系列教程17-继承-独立表-TABLE_PER_CLASS
PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...
- JPA 系列教程16-继承-联合子类-JOINED
联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...
- JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...
- JPA 系列教程12-复合主键-2个@Id+@IdClass
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id一样 Airline p ...
随机推荐
- 1、<img />标签
alt:当图片不显示时的文字说明 title:鼠标悬停在图片上的出现的文字说明
- LeetCode 385. Mini Parse
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Mybatis的传参
最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发 最终要的一点事,自己写的话,记忆更加深刻: 首先自己先写了个静态块,防止代码冗余: private static Sq ...
- Documention
Object.bool Does the object exist? Object.name Components share the same name with the game object a ...
- Applet签名
applet签名 1.生成密匙库 keytool -genkey -keystore mytest.store -alias mytest -validity 365 -keystore 密匙库 -a ...
- ZOJ 1926 Guessing Game
#include<cstdio> ],s2[]; ]; ]; int bz; int main() { int n,i; while(~scanf("%d",& ...
- Mr. Kitayuta vs. Bamboos
Mr. Kitayuta vs. Bamboos 题目链接:http://codeforces.com/problemset/problem/505/E 参考:http://blog.csdn.net ...
- wangEditor的jQuery插件化
wangEditor是一款优秀的Web富文本编辑器.这篇随笔中讲述的wangEditor版本是2.1.22,由于它依赖于jQuery(作者打算在第三版中取消对jQuery的依赖),那么如果能使用$(& ...
- SQLSERVER异机备份
/* 作者:landv 功能:异机备份 开发时间:2016年7月2日 15:27:08 */ ) drop procedure [dbo].[p_backupdb] GO create proc p_ ...
- Retrofit,Rxjava,OkHttp3的配置
这几个库的版本都更新了,和以前的使用略有不同,这是两篇介绍的博客:http://www.jianshu.com/p/91ac13ed076d ,https://drakeet.me/retrofit- ...