package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyEnumerated;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@NamedQueries(@NamedQuery(name = "get_person_by_name", query = "select p from Person p where name = :name"))
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private String nickName;
private String address;
@Temporal(TemporalType.TIMESTAMP)
private Date createdOn;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
private List<Phone> phones = new ArrayList<>();
@ElementCollection
@MapKeyEnumerated(EnumType.STRING)
private Map<AddressType, String> addresses = new HashMap<>();
@Version
private int version;
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 String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public List<Phone> getPhones() {
return phones;
}
public void setPhones(List<Phone> phones) {
this.phones = phones;
}
public Map<AddressType, String> getAddresses() {
return addresses;
}
public void setAddresses(Map<AddressType, String> addresses) {
this.addresses = addresses;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
package org.example.demo.hibernate;
public enum AddressType {
HOME, OFFICE
}
package org.example.demo.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class Partner {
@Id
@GeneratedValue
private Long id;
private String name;
@Version
private int version;
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 int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.MapKeyTemporal;
import javax.persistence.OneToMany;
import javax.persistence.TemporalType;
@Entity
public class Phone {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Person person;
@Column(name = "phone_number")
private String number;
@Enumerated(EnumType.STRING)
@Column(name = "phone_type")
private PhoneType type;
@OneToMany(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Call> calls = new ArrayList<>();
@OneToMany(mappedBy = "phone")
@MapKey(name = "timestamp")
@MapKeyTemporal(TemporalType.TIMESTAMP)
private Map<Date, Call> callHistory = new HashMap<>();
@ElementCollection
private List<Date> repairTimestamps = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public PhoneType getType() {
return type;
}
public void setType(PhoneType type) {
this.type = type;
}
public List<Call> getCalls() {
return calls;
}
public void setCalls(List<Call> calls) {
this.calls = calls;
}
public Map<Date, Call> getCallHistory() {
return callHistory;
}
public void setCallHistory(Map<Date, Call> callHistory) {
this.callHistory = callHistory;
}
public List<Date> getRepairTimestamps() {
return repairTimestamps;
}
public void setRepairTimestamps(List<Date> repairTimestamps) {
this.repairTimestamps = repairTimestamps;
}
}
package org.example.demo.hibernate;
public enum PhoneType {
LAND_LINE, MOBILE;
}
package org.example.demo.hibernate;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "phone_call")
public class Call {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Phone phone;
@Column(name = "call_timestamp")
private Date timestamp;
private int duration;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
package org.example.demo.hibernate;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Payment {
@Id
@GeneratedValue
private Long id;
private BigDecimal amount;
private boolean completed;
@ManyToOne
private Person person;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package org.example.demo.hibernate;
import javax.persistence.Entity;
@Entity
public class CreditCardPayment extends Payment {
}
package org.example.demo.hibernate;
import javax.persistence.Entity;
@Entity
public class WireTransferPayment extends Payment {
}
创建对应的表
drop table if exists CreditCardPayment cascade;
drop table if exists Partner cascade;
drop table if exists Payment cascade;
drop table if exists Person cascade;
drop table if exists Person_addresses cascade;
drop table if exists Phone cascade;
drop table if exists phone_call cascade;
drop table if exists Phone_repairTimestamps cascade;
drop table if exists WireTransferPayment cascade;
drop sequence if exists hibernate_sequence;
create sequence hibernate_sequence start 1 increment 1;
create table CreditCardPayment (id int8 not null, primary key (id));
create table Partner (id int8 not null, name varchar(255), version int4 not null, primary key (id));
create table Payment (id int8 not null, amount numeric(19, 2), completed boolean not null, person_id int8, primary key (id));
create table Person (id int8 not null, address varchar(255), createdOn timestamp, name varchar(255), nickName varchar(255), version int4 not null, primary key (id));
create table Person_addresses (Person_id int8 not null, addresses varchar(255), addresses_KEY varchar(255) not null, primary key (Person_id, addresses_KEY));
create table Phone (id int8 not null, phone_number varchar(255), phone_type varchar(255), person_id int8, order_id int4, primary key (id));
create table phone_call (id int8 not null, duration int4 not null, call_timestamp timestamp, phone_id int8, primary key (id));
create table Phone_repairTimestamps (Phone_id int8 not null, repairTimestamps timestamp);
create table WireTransferPayment (id int8 not null, primary key (id));
alter table CreditCardPayment add constraint FKgvx0a4wvrjtoi07uuxpyxxcnb foreign key (id) references Payment;
alter table Payment add constraint FKdti325lhmgfbhps30sp20vjtu foreign key (person_id) references Person;
alter table Person_addresses add constraint FKbs25rlvs45qpe83fidi1x1nj0 foreign key (Person_id) references Person;
alter table Phone add constraint FKmw13yfsjypiiq0i1osdkaeqpg foreign key (person_id) references Person;
alter table phone_call add constraint FKk2aiqu1u6oenmvldor9mjun1p foreign key (phone_id) references Phone;
alter table Phone_repairTimestamps add constraint FK4pbl8ax9vgrstfenjwyjuxwbd foreign key (Phone_id) references Phone;
alter table WireTransferPayment add constraint FK9nnnmyet06yjqd3ccrl46qxe foreign key (id) references Payment;
然后使用如下的 src/main/resources/META-INF/persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="test">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
</properties>
</persistence-unit>
</persistence>
不再每次 drop and create tables