org.hibernate.MappingException:无法确定:java.util.List的类型,在表:user,对于列:[org.hibernate.mapping.Column(events)]

时间:2016-03-10 09:41:45

标签: java spring hibernate

它给我以下错误。

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)]

这是我的代码

User.java:

import com.google.common.base.Objects;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
public class User {

    @Id
    @NotNull
    @Size(max = 64)
    @Column(name = "id", nullable = false, updatable = false)
    private String id;

    @NotNull
    @Size(max = 64)
    @Column(name = "name", nullable = false)
    private String name;

    @NotNull
    @Size(max = 64)
    @Column(name = "firstname", nullable = false)
    private String firstname;

    @NotNull
    @Size(max = 64)
    @Column(name = "email", nullable = false)
    private String email;

    @NotNull
    @Size(max = 64)
    @Column(name = "password", nullable = false)
    private String password;


    private List<Events> events;

    public User() {
    }

    public User(String id, String name, String firstname, String email, String password) {
        super();
        this.id = id;
        this.name = name;
        this.firstname = firstname;
        this.email = email;
        this.password = password;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Events> getEvents() {
        return events;
    }

    public void setEvents(List<Events> events) {
        this.events = events;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("id", id).add("name", name).add("firstname", firstname)
                .add("email", email).add("password", password).add("events", events).toString();
    }
}

Events.java:

public class Events {

    public Events() {
    }

    private String startEvent;
    private String endEvent;

    public String getStartEvent() {
        return startEvent;
    }

    public void setStartEvent(String startEvent) {
        this.startEvent = startEvent;
    }

    public String getEndEvent() {
        return endEvent;
    }

    public void setEndEvent(String endEvent) {
        this.endEvent = endEvent;
    }

}

事件不应存储在数据库中

2 个答案:

答案 0 :(得分:3)

从代码的外观来看,在我看来,事件不应该存储在数据库中。

如果是这种情况,那么将它们标记为@Transient

就足够了
@Transient
private List<Events> events;

答案 1 :(得分:2)

如果需要将events属性存储在数据库中,则需要添加映射。

@OneToMany
private List<Events> events;

@OneToMany
@JoinColumn
private List<Events> events;

并将@Entity注释添加到Events

@Entity
public class Events {

}