我试图在项目中第一次使用JPA。我的大多数实体工作正常,但我遇到了一个加入继承策略的问题。实体也被杰克逊序列化,所以他们也有Json注释。
父母"用户"类: (编辑:添加"输入"字段)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonTypeName("user")
@JsonSubTypes({
@JsonSubTypes.Type(name="customer", value=Customer.class),
@JsonSubTypes.Type(name="employee", value=Employee.class)})
@Entity(name = "User")
@Table(name="user")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="type",discriminatorType = DiscriminatorType.INTEGER)
@NamedQuery(name="User.all",query = "select u from User u")
public abstract class User {
@Id
private String username;
@Column(name = "type",nullable = false)
private int type;
public User(){
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public abstract Set<Order> getOrders();
}
儿童&#34;员工&#34;
@JsonTypeName("employee")
@Entity(name="Employee")
@Table(name="employee")
@PrimaryKeyJoinColumn(name = "username",referencedColumnName = "username")
@DiscriminatorValue("1")
@NamedQuery(name = "Employee.all",query = "select e from Employee e")
public class Employee extends User implements Serializable{
private String username;
private String firstName;
private String lastName;
private String email;
@Convert(converter = LocalDatePersistenceConverter.class)
private LocalDate dateStarted;
@Convert(converter = LocalDatePersistenceConverter.class)
private LocalDate dateEnded;
@OneToMany(mappedBy = "employee",targetEntity = Order.class,fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)
@JsonIgnore
private Set<Order> orders = new HashSet<>();
public Employee() {
}
@Override
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void addOrder(Order order){
orders.add(order);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getDateStarted() {
if(dateStarted != null)
return dateStarted.toString();
else return null;
}
public void setDateStarted(LocalDate dateStarted) {
this.dateStarted = dateStarted;
}
public String getDateEnded() {
if(dateEnded != null)
return dateEnded.toString();
else return null;
}
public void setDateEnded(LocalDate dateEnded) {
this.dateEnded = dateEnded;
}
@Override
public String toString(){
return getUsername();
}
}
小孩&#34;顾客&#34;: (编辑:删除@Id字段)
@JsonTypeName("customer")
@Entity(name="Customer")
@Table(name="customer")
@PrimaryKeyJoinColumn(name = "username",referencedColumnName = "username")
@DiscriminatorValue("2")
@NamedQueries({
@NamedQuery(name="Customer.all",query = "select c from Customer c")
})
public class Customer extends User implements Serializable{
public enum VIP_TYPE {NORMAL,SILVER,GOLD,DIAMOND}
@Transient
private static final int SILVER_THRESHOLD = 1000;
@Transient
private static final int GOLD_THRESHOLD = 2000;
@Transient
private static final int DIAMOND_THRESHOLD = 3000;
private String firstName;
private String lastName;
private String email;
private String address;
private String postcode;
private String mobileNumber;
private String homeNumber;
@Convert(converter = VipTypeConverter.class)
private VIP_TYPE vipGroup;
private String discount;
@OneToMany(mappedBy = "customer",targetEntity = Order.class,fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JsonIgnore
private Set<Order> orders = new HashSet<>();
public Customer() {
}
@Override
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public void addOrder(final Order order){
orders.add(order);
updateVipGroup();
}
private void updateVipGroup() {
int sum = orders.stream().map(Order::getPayment).distinct().mapToInt(p->p.getAmmount()).sum();
if(sum > DIAMOND_THRESHOLD){
vipGroup = VIP_TYPE.DIAMOND;
return;
}
if(sum > GOLD_THRESHOLD){
vipGroup = VIP_TYPE.GOLD;
return;
}
if(sum > SILVER_THRESHOLD){
vipGroup = VIP_TYPE.SILVER;
return;
}
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public void setVipGroup(VIP_TYPE vipGroup) {
this.vipGroup = vipGroup;
}
public void setHomeNumber(String homeNumber) {
this.homeNumber = homeNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getDiscount() {
return discount;
}
public VIP_TYPE getVipGroup() {
return vipGroup;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getAddress() {
return address;
}
public String getPostcode() {
return postcode;
}
public String getMobileNumber() {
return mobileNumber;
}
public String getHomeNumber() {
return homeNumber;
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="local" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/cod</jta-data-source>
<class>com.technicalpioneers.cod.user.Customer</class>
<class>com.technicalpioneers.cod.user.Employee</class>
<class>com.technicalpioneers.cod.user.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
所有与&#34;员工&#34;工作文件,我可以使用命名查询Employee.all
来查找数据库中的所有员工。
但是,如果我尝试检索任何客户,我会收到错误。如果我尝试运行命名查询Customer.all
,我会得到:
java.lang.IllegalArgumentException: NamedQuery of name: Customer.all not found.
如果我尝试使用EntityManager的find()方法找到我得到的特定客户:
javax.servlet.ServletException: Exception [EclipseLink-43] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing class for indicator field value [2] of type [class java.lang.Integer].
Descriptor: RelationalDescriptor(com.technicalpioneers.cod.user.User --> [DatabaseTable(user)])
我不明白为什么JPA找不到客户实体。我已经检查了用户表和&#34;类型&#34;列是正确的数字,并正确设置@DescriminatorValue。它几乎就像注释被忽略了一样?
做了很多干净的重建并重新组装。非常感谢任何帮助!
答案 0 :(得分:1)
我最终发现了这一点。 https://bugs.eclipse.org/bugs/show_bug.cgi?id=429992 事实证明,EclipseLink将默默地忽略具有lambda表达式的实体!非常讨厌,至少在日志中没有提到它!
感谢所有花时间的人!