gpt4 book ai didi

java - Hibernate 无法确定集合的类型

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:06 26 4
gpt4 key购买 nike

我收到如下错误:

org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: customers, for columns: [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();
}

人物实体

@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;
}
}

联系实体

@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;
}
}

客户联系方式

是否可以向具有泛型类型的父类添加抽象方法,然后使用所选类型覆盖它?

最佳答案

那是因为您使用的是@Id注释。您应该将 JPA 注释放在字段本身而不是 getter 上,

@OneToMany(targetEntity = CustomerContact.class, mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
private Collection<CustomerContact> contacts;

关于java - Hibernate 无法确定集合的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com