I fear this is a very common problem, but I was not able to solve it anyway -.- I want to organize events and locations. At one location several events can be conducted.
It boils down to these lines:
Location:
@OneToMany(fetch = FetchType.LAZY, mappedBy="location")
private List<Event> events = new ArrayList<Event>();
Event:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id")
private Location location;
I always get this error:
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: com.mycompany.domain.Location, at table: Event, for columns: [org.hibernate.mapping.Column(location)]
I honestly don't see the error.... thx in advance
The whole code:
Event:
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;
}
}
Location:
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;
}
}
You have to adopt just one way to put annotations (@Column, @Id, @ManyToOne ...) for all your entities: choose between putting annotations on attributes or on getters. Don't mixt it: that means don't put annotations sometimes on attributes and sometimes on getters. Otherwise, you'll got this error.