4

我担心这是一个非常普遍的问题,但无论如何我都无法解决 -.- 我想组织活动和地点。在一个地点可以举办多个活动。

它归结为以下几行:

地点:

@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,在表:事件,列:[org.hibernate.mapping.Column(location)]

老实说,我没有看到错误....提前谢谢

整个代码:

事件:

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;
}
}
4

1 回答 1

11

您必须采用一种方式为所有实体添加注释(@Column、@Id、@ManyToOne ...):在属性或 getter 上进行选择。不要混淆它:这意味着不要将注释有时放在属性上,有时放在 getter 上。否则,您将收到此错误。

于 2016-04-02T16:56:12.000 回答