org.hibernate.MappingException:无法确定类型:java.util.List,在表:用户,列:[org.hibernate.mapping.Column(事件)]
Posted
技术标签:
【中文标题】org.hibernate.MappingException:无法确定类型:java.util.List,在表:用户,列:[org.hibernate.mapping.Column(事件)]【英文标题】:org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(events)] 【发布时间】:2016-03-10 09:41:45 【问题描述】:它给了我以下错误。
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;
事件不应存储在数据库中
【问题讨论】:
【参考方案1】:从代码的外观来看,在我看来,事件不应该存储在数据库中。
如果是这种情况,那么将它们标记为@Transient 就足够了
@Transient
private List<Events> events;
【讨论】:
然后'@Transient'注解表示该属性或方法没有持久化到数据库中并且仍然可以保留在那里。由于 '@Id' 在属性上,因此只保留在属性上应该没问题,但您可能还需要将它添加到 getter 和 setter 中。 @JakubBalhar 不需要将@Transient
添加到setter。
感谢您的澄清。【参考方案2】:
如果需要将 events
属性存储在数据库中,则需要添加映射。
@OneToMany
private List<Events> events;
或
@OneToMany
@JoinColumn
private List<Events> events;
并将@Entity
注解添加到Events
类
@Entity
public class Events
【讨论】:
以上是关于org.hibernate.MappingException:无法确定类型:java.util.List,在表:用户,列:[org.hibernate.mapping.Column(事件)]的主要内容,如果未能解决你的问题,请参考以下文章