无法确定表中<datatype>的类型:TableX,列为org.hibernate.mapping.Column(userPrefs)

时间:2016-04-15 00:07:46

标签: java hibernate

我在堆栈溢出时搜索了这个,并结合了将注释放在字段或getter上的建议,但仍然看到问题所以发布。

我有一个用户表和首选项表。这是架构

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)
)
;

我希望能够在我的用户POJO中编写一个方法来检索该用户的所有首选项。这就是我所拥有的。

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

 }

这是我的用户偏好类:

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


}

我得到的错误是这样的: 引起:org.hibernate.MappingException:无法确定类型:java.util.Set,在表:user,对于列:[org.hibernate.mapping.Column(userPrefs)]

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。我不知道为什么当UserUserPreference是主键时,为prefIduserId设置了单独的ID。您应该在User而不是user中使用名称为UserPreferences的{​​{1}}类型属性。这就是Long userIdmappedBy="user"中指出的内容。同时将注释移动到getter。我改变了你的课程以使它工作。这是更新的代码。

User.java

User

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 = "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;
    }

}

UserPreferences.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;
    }
}

为简单起见,我删除了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; } } writeExternal方法。以下是获取用户偏好的用法。

readExternal