I have problem to create two associated tables with hibernate in a PostgreSQL database. I am inclined to believe that the problem lies in the Database (configuration?) since I have tried nearly all the Hibernate approaches proposed in Internet sites and I receive still the same error. Is there any configuration demanded before using a PostgreSql database? I cannot explain in other way the error which appears:
"Relation department does not exist"
I Join the two Entities Employee and Department as follows:
@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;
I am trying to create the tables with a couple of records in the main method as:
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("Sales");
session.persist(department);
Employee emp1 = new Employee("Nina", "Mayers", "111");
Employee emp2 = new Employee("Tony", "Almeida", "222");
emp1.setDepartment(department);
emp2.setDepartment(department);
session.persist(emp1);
session.persist(emp2);
session.getTransaction().commit();
session.close();
The error comes up on the line : session.persist(department);
Is anyone experienced in PostgreSql Databases, to help me to find why it cannot recognize the tables/relations and it comes up with the error:
"Relation department does not exist" ?
UPDATE:
I have looked at the log file. After the usual message coming out from Eclipse as well, the last line of the message is: "No connection could be made because the target machine actively refused it". I deduce that the problem is probably at the connection. Are there any properties, which I should set (in pgAdmin) in order to achieve the connection? I have set the line
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testDB</property> in cfg.xml file in Eclipse.
"DEPARTMENT"and"department", they probably should both have the same case.