简体   繁体   中英

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. 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)]

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. You should have a User type property with name as user in UserPreferences instead of Long userId . That is what mappedBy="user" indicate in the User class. Also move the annotation to getter. I changed both your classes to make it work. Here is the updated code.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM