[英]could not determine type for <DataType> at table: TableX, for columns org.hibernate.mapping.Column(userPrefs)
I searched for this on stack overflow and incorporated the suggestion of putting the annotation over fields or getters but still see the issue so posting. 我在堆栈溢出时进行了搜索,并纳入了将注释放在字段或获取器上的建议,但仍然看到问题,因此发布了。
I have a user table and preference table. 我有一个用户表和首选项表。 Here's the schema
这是架构
create table userpreferences(
ID bigint auto_increment
user_id bigint not null,
preference_id bigint not null,
preference_value varchar(255),
PRIMARY KEY(ID)
)
;
create table user(
user_id bigint auto_increment,
user_name varchar(255) not null,
primary key(user_id)
)
;
I want to be able to write a method in my user POJO to retrieve all Preferences for that user. 我希望能够在用户POJO中编写一个方法来检索该用户的所有首选项。 Here's what I have.
这就是我所拥有的。
@Entity
@Table(name="user")
public class User extends KeyedEntity {
private Long user_id;
private String userName;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<UserPreferences> userPrefs = new HashSet<UserPreferences>();
/**
* @return Returns the userName.
*/
@Column(name="USER_NAME")
public String getUserName() {
return this.userName;
}
/**
* @param userName The userName to set.
*/
public void setUserName(String userName) {
this.userName = userName;
}
@Id @Column(name="user_id") @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return this.user_id;
}
@Override
public void setId(Long id) {
super.setId(id);
}
/**
* Lazy fetch of the set of application attributes this user has set.
* @return
*/
public Set<UserPreferences> getUserPrefs(){
return new HashSet<UserPreferences>(this.userPrefs);
}
/**
* Setter for the user's attributes.
*
* @param userAttributes
*/
public void setUserPrefs(Set<UserPreferences> userPrefs){
this.userPrefs.clear();
this.userPrefs.addAll(userPrefs);
}
}
Here's my user preference class: 这是我的用户偏好设置类:
@Entity
@Table(name="userpreferences")
public class UserPreferences extends KeyedEntity implements Externalizable, Cloneable {
private static final int VERSION = 1;
private Long prefId;
private Long userId;
private String prefValue;
@Column(name="PREFERENCE_ID")
public Long getPrefId() {
return prefId;
}
public void setPrefId(Long prefId) {
this.prefId = prefId;
}
@Column(name="USER_ID")
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Column(name="PREFERENCE_VALUE")
public String getPrefValue() {
return prefValue;
}
public void setPrefValue(String prefValue) {
this.prefValue = prefValue;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeShort(VERSION);
out.writeObject(this.prefId);
out.writeObject(this.userId);
out.writeObject(this.prefValue);
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
short version = in.readShort();
if(version>=0){
this.prefId = in.readLong();
this.userId = in.readLong();
this.prefValue =(String) in.readObject();
}
}
@Id @Column(name="ID")
@GeneratedValue(strategy=GenerationType.AUTO)
@Override
public Long getId() {
return id;
}
public void setId(Long id) {
super.setId(id);
}
}
The error I am getting is this: Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: user, for columns: [org.hibernate.mapping.Column(userPrefs)] 我得到的错误是:原因:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:用户,对于列:[org.hibernate.mapping.Column(userPrefs)]
There are several issues with your code. 您的代码有几个问题。 I don't know why you have a separate id for
User
and UserPreference
when prefId
and userId
are the primary keys. 我不知道为什么当
prefId
和userId
是主键时,您对于User
和UserPreference
有一个单独的ID。 You should have a User
type property with name as user
in UserPreferences
instead of Long userId
. 您应该在
UserPreferences
拥有一个名称为user
的User
type属性,而不是Long userId
。 That is what mappedBy="user"
indicate in the User
class. 那就是
mappedBy="user"
在User
类中指示的内容。 Also move the annotation to getter. 还将注释移至getter。 I changed both your classes to make it work.
我更改了两个班级以使其正常运行。 Here is the updated code.
这是更新的代码。
User.java User.java
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User {
private Long user_id;
private String userName;
private Set<UserPreferences> userPrefs;
/**
* @return Returns the userName.
*/
@Column(name = "USER_NAME")
public String getUserName() {
return this.userName;
}
/**
* @param userName
* The userName to set.
*/
public void setUserName(String userName) {
this.userName = userName;
}
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return this.user_id;
}
public void setId(Long id) {
this.user_id = id;
}
/**
* Lazy fetch of the set of application attributes this user has set.
*
* @return
*/
@OneToMany(mappedBy = "user")
public Set<UserPreferences> getUserPrefs() {
return this.userPrefs;
}
/**
* Setter for the user's attributes.
*
* @param userAttributes
*/
public void setUserPrefs(Set<UserPreferences> userPrefs) {
this.userPrefs = userPrefs;
}
}
Preference.java Preference.java
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "preference")
public class Preference {
private Long pref_id;
private String prefName;
private Set<UserPreferences> userPrefs;
@Id
@Column(name = "pref_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return pref_id;
}
public void setId(Long id) {
this.pref_id = id;
}
@Column(name = "PREF_NAME")
public String getPrefName() {
return prefName;
}
public void setPrefName(String prefName) {
this.prefName = prefName;
}
@OneToMany(mappedBy = "preference")
public Set<UserPreferences> getUserPrefs() {
return userPrefs;
}
public void setUserPrefs(Set<UserPreferences> userPrefs) {
this.userPrefs = userPrefs;
}
}
UserPreferences.java UserPreferences.java
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "userpreferences")
public class UserPreferences {
private Long id;
private User user;
private Preference preference;
private String prefValue;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "PREFERENCE_VALUE")
public String getPrefValue() {
return prefValue;
}
public void setPrefValue(String prefValue) {
this.prefValue = prefValue;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="user_id")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="pref_id")
public Preference getPreference() {
return preference;
}
public void setPreference(Preference preference) {
this.preference = preference;
}
@Override
public String toString() {
return this.prefValue;
}
}
I removed writeExternal
and readExternal
methods for simplicity. 为了简单起见,我删除了
writeExternal
和readExternal
方法。 And here is the usage in order to get the user preferences. 这是为了获得用户首选项的用法。
// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
// creating session object
Session session = factory.openSession();
// creating transaction object
Transaction t = session.beginTransaction();
User u1 = new User();
u1.setUserName("Jo");
User u2 = new User();
u2.setUserName("Nick");
Preference p = new Preference();
p.setPrefName("mapping");
UserPreferences up1 = new UserPreferences();
up1.setPreference(p);
up1.setUser(u1);
up1.setPrefValue("ManyToMany");
session.save(up1);
UserPreferences up2 = new UserPreferences();
up2.setPreference(p);
up2.setUser(u2);
up2.setPrefValue("OneToMany");
session.save(up2);
t.commit();// transaction is committed
session.close();
session = factory.openSession();
// creating transaction object
t = session.beginTransaction();
Long u1id = u1.getId();
Long u2id = u2.getId();
System.out.println(u1id + ", " + u2id);
User user = (User) session.get(User.class, u1id);
System.out.println(user.getUserName() + ", " + user.getUserPrefs());
user = (User) session.get(User.class, u2id);
System.out.println(user.getUserName() + ", " + user.getUserPrefs());
t.commit();// transaction is committed
session.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.