我有一個名為 "User "的物體類,我想在User中存盤一個資料串列。
這是我的代碼 :
@Entity
public class User extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@Column(unique = true)/span>
private String mail;
private String password;
private List<Feature> features;
}
我有一個Feature類,但它不是一個物體,因為它不需要成為一個物體類,我想.
。public class Feature {
private String fName;
private Long fYear;
}
當我啟動應用程式時,我得到一個錯誤:
org.springframework.beans.factory.BeanCreationException。創建Bean with name 'entityManagerFactory'時出錯,該定義in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration. class] 。呼叫of init方法失敗;嵌套的例外是 javax.persistence.PersistenceException。[PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(features)]。
uj5u.com熱心網友回復:
你不能把串列作為一種型別插入到SQL表中。
你需要定義關系:
你需要定義關系。
@Entity
public class User extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@Column(unique = true)
private String mail。
private String password;
@OneToMany (或任何其他符合你case的關系)
private List<Feature> features。
}
和
@Entity
public class Feature {
@Id
//在此定義生成策略。
private long id;
private String fName;
private Long fYear;
}
uj5u.com熱心網友回復:
由于Feature型別確實持有映射到域資料的實體欄位,它應該被保存為一個單獨的物體。
如果Feature仍然在您的領域邊界之外,并且不需要與User一起存盤,您可以將其標記為@Transient,并且您可能需要在加載用戶物體時根據您的應用程式語意來加載其資料。
否則,你將采用傳統的路徑來映射你的物體:
@Entity
public class Feature extends BaseEntity{
//span> ...
@ManyToMany(mappedBy="features")
private Set<User> users = new HashSet< > ();
//...。
然后你將需要定義User <> Feature映射:
@Entity
public class User extends BaseEntity {
/ ...
@ManyToMany[/span
@JoinTable(name="USER_FEATURE",
欄目=
@JoinColumn(name="USER_ID", referencedColumnName="ID") 。
inverseJoinColumns=
@JoinColumn(name="FEATURE_ID"/span>, referencedColumnName="ID"/span>)
)
private Set<Feature> features = new HashSet<>()。
// ...。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/327135.html
標籤:
上一篇:用休眠/春季啟動連接3個表
