2013-10-30 59 views
3

我擔心這是一個非常普遍的問題,但我無法解決它 - - 我想組織事件和位置。在一個地點可以進行幾個事件。休眠「無法確定類型...」ManyToOne OneToMany映射

它歸結爲以下行:

地點:

@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.Locat的類型離子,在表:事件,對於列:[org.hibernate.mapping.Column(位置)]

我真的不看到錯誤.... 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; 
} 
} 
+2

你應該把你的註釋放在getter方法上,而不是放在字段上。 –

+0

我以前總是在田裏注意自己的註釋。這從來都不是問題。我試過你的建議不多,但仍然得到同樣的錯誤。 – user2158143

+0

好吧,我解決了這個問題。代碼中的任何更改都不會生效,因爲我必須使用maven重新編譯項目。 - 這花了我幾個小時。現在我正式討厭Maven。 – user2158143

回答

5

你得結結實實pt只是一種爲所有實體放置註釋(@Column,@Id,@ManyToOne ...)的方法:選擇將註釋放在屬性上還是放在getter上。不要混淆它:這意味着不要將註釋有時放在屬性上,有時放在getter上。否則,你會得到這個錯誤。