arjacsoh
arjacsoh

Reputation: 9252

Error on defining association with Hibernate

I am trying to create two associated tables in a PostgreSQL database using Hibernate. The Entities I have written are:

@Entity
@Table(name="EMPLOYEE")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="employee_id")
private Long employeeId;
.
.
.

@ManyToOne
@JoinColumn(name="department_id")
public Department getDepartment() {
    return department;
}

public void setDepartment(Department department) {
    this.department = department;
}

private Department department;

And:

@Entity
@Table(name="DEPARTMENT")
public class Department implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="DEPARTMENT_ID")
private Long departmentId;

.
.
.
@OneToMany(mappedBy="department")
public Set<Employee> getEmployees() {
    return employees;
}

public void setEmployees(Set<Employee> employees) {
    this.employees = employees;
}


private Set<Employee> employees;

My first question is what is the diffence if I set the @ManyToOne or the @OneTOMany annotations on the member variable or on the method. I am almost sute that it affects Entities but cannot remember how. I am asking that, because it produces two different messages by tthe error. If I set the annotations as above the error is:

" org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: DEPARTMENT, for columns: [org.hibernate.mapping.Column(employees)]"

If I set the annotations on the member variables instead of the methods, then the error which comes up is:

"Relation department does not exists".

If I change the @OneToMany annotation to @OneToMany(mappedBy="DEPARTMENT") then the error is :

" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.al.hibernatetest.Employee.DEPARTMENT".

What is wrong and it cannot create the tables as described by the annotations? What should be changed?

UPDATE:

The error which comes out is (it contains some German meaning: "Relation department does not exist"):

ERROR: FEHLER: Relation »department« existiert nicht
  Position: 13
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2967)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3478)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:205)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:190)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    at com.al.hibernatetest.Main.main(Main.java:21)
Caused by: org.postgresql.util.PSQLException: FEHLER: Relation »department« existiert nicht
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    ... 22 more

Upvotes: 0

Views: 2595

Answers (1)

German
German

Reputation: 3606

You need to declare all the annotations in the member variables OR all the annotations in the methods, any should work. In the example you gave you mixed both and that's the reason for "Could not determine type for: java.util.Set". You can mix them but using @Access(AccessType.FIELD/PROPERTY) if required.

In your example, @OneToMany needs the "mappedBy" attribute because there is an opposite @ManyToOne in the other entity. mappedBy takes the name of the field (or property) annotated with @ManyToOne in the other entity, it doesn't take the name of the column in the database. That's the reason of the error "mappedBy reference an unknown target entity property: com.al.hibernatetest.Employee.DEPARTMENT", the exception implies that the mappedBy was written in upper case, but the actual name in the java class for that property seems lower case.

About the error "Relation department does not exists" I don't know exactly how would the entities look like to produce it.

Upvotes: 1

Related Questions