我担心这是一个非常普遍的问题,但无论如何我都无法解决它 - .- 我想组织活动和地点。在一个地方可以举办几个活动。
归结为以下几点:
位置:
@OneToMany(fetch = FetchType.LAZY, mappedBy="location")
private List<Event> events = new ArrayList<Event>();
事件:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id")
private Location location;
我总是收到这个错误:
初始SessionFactory创建failed.org.hibernate.MappingException:无法确定:com.mycompany.domain.Location的类型,在表:Event,对于列:[org.hibernate.mapping.Column(location)]
老实说,我没有看到错误.... thx提前
整个代码:
事件:
package com.mycompany.domain;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
@Entity
@Table
public class Event {
@Id
@GeneratedValue
private Long id;
private String name;
private Date date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id")
private Location location;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "event")
private List<UserAttendsEvent> userAttendsEvents = new ArrayList<UserAttendsEvent>() ;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "event")
private List<Invoice> invoices = new ArrayList<Invoice>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public List<Invoice> getInvoices() {
return invoices;
}
public void setInvoices(List<Invoice> invoices) {
this.invoices = invoices;
}
public List<UserAttendsEvent> getUserAttendsEvents() {
return userAttendsEvents;
}
public void setUserAttendsEvents(List<UserAttendsEvent> userAttendsEvents) {
this.userAttendsEvents = userAttendsEvents;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
位置:
package com.mycompany.domain;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
@Entity
@Table
public class Location {
@Id
@GeneratedValue
private Long id;
private String name;
private String adress;
@OneToMany(fetch = FetchType.LAZY, mappedBy="location")
private List<Event> events = new ArrayList<Event>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}
答案 0 :(得分:7)
您必须采用一种方法为所有实体添加注释(@Column,@ Id,@ ManyToOne ...):选择在属性或getter上添加注释。不要混淆:这意味着有时不会在属性上添加注释,有时也会在getter上添加注释。否则,您将收到此错误。