我收到的错误如下:
org.hibernate.MappingException:无法确定:java.util.Collection的类型,在表:customers,对于列:[org.hibernate.mapping.Column(contacts)]
我的代码如下:
@Setter
@MappedSuperclass
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public abstract class PersonEntity<C extends ContactEntity> extends BaseEntity {
protected String firstName;
protected String lastName;
@Column(name = FIRST_NAME_COLUMN, nullable = false)
public String getFirstName() {
return firstName;
}
@Column(name = LAST_NAME_COLUMN, nullable = false)
public String getLastName() {
return lastName;
}
public abstract Collection<C> getContacts();
}
PersonEntity
@Setter
@MappedSuperclass
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public abstract class ContactEntity extends BaseEntity {
private String contact;
@Column(name = CONTACT_COLUMN, nullable = false)
public String getContact() {
return contact;
}
}
客户
@Entity
@Setter
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table(name = TABLE_NAME)
public class Customer extends PersonEntity<CustomerContact> implements User {
private Collection<CustomerContact> contacts;
@Id
@Override
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
@SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME, allocationSize = 1)
public Long getId() {
return this.id;
}
@Override
@OneToMany(targetEntity = CustomerContact.class, mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
public Collection<CustomerContact> getContacts() {
return contacts;
}
}
ContactEntity
@Entity
@Setter
@NoArgsConstructor
@Table(name = TABLE_NAME)
@ToString(exclude = "customer")
public class CustomerContact extends ContactEntity {
private Customer customer;
@Id
@Override
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
@SequenceGenerator(name = SEQUENCE_NAME, sequenceName = SEQUENCE_NAME, allocationSize = 1)
public Long getId(){
return this.id;
}
@ManyToOne
@JsonIgnore
@JoinColumn(name = CUSTOMER_COLUMN, nullable = false)
public cCustomer getCustomer() {
return customer;
}
}
CustomerContact
是否无法使用泛型类型向父类添加抽象方法,然后使用所选类型覆盖它?
答案 0 :(得分:0)
这是因为您正在使用@Id
注释。您应该将JPA注释放在字段本身而不是getter上,
@OneToMany(targetEntity = CustomerContact.class, mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
private Collection<CustomerContact> contacts;