4

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;
}
}
user2158143
  • 123
  • 1
  • 2
  • 10
  • 2
    You should put your annotations on getter methods not on fields themselves. – Ean V Oct 30 '13 at 05:14
  • I always had the annotations on the fields themselves in the past. It has never been a problem. I tried your suggestion non the less, but still get the same error. – user2158143 Oct 30 '13 at 12:48
  • Ok I solved the problem. No changes in code took effect because I had to rebuild the project with maven. -.- This took me hours. Now I officially hate Maven. – user2158143 Oct 30 '13 at 14:11
  • 2
    I had the same problem and moving the annotation to the getter helped. Rebuilding with maven alone didn't help. – mirelon Feb 19 '14 at 14:52
  • 2
    I had this same error. Turns out I had the id-annotations on the field and the rest of the annotations on the get methods – Tommy May 19 '14 at 11:08
  • @Tommy 's comment worked – nobalG Dec 17 '14 at 10:46
  • Same as what @Tommy said. Mixing annotations on fields and methods. In my case the concrete class was extending an abstract class with common methods with method-level annotations, so was even more tricky to see what was going on. – uncrase Mar 04 '15 at 12:06

1 Answers1

10

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.

Salim Hamidi
  • 18,947
  • 1
  • 20
  • 28