-1

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.

12
  • 1
    Most like this is case folding. Unquoted identifiers are folded to lower case. Many access libs auto-quote for you, while things like psql do not. Commented Apr 26, 2013 at 19:04
  • Can you clarify your comment and make your suggestion what could be done? I cannot catch it as you express it. Thanks. Commented Apr 26, 2013 at 19:11
  • You have both "DEPARTMENT" and "department", they probably should both have the same case. Commented Apr 26, 2013 at 19:31
  • @muistooshort: I have tried to write all either lower or upper case, but it does not change anything. Unfortunately this obvious case does not eliminate the problem. Commented Apr 26, 2013 at 19:38
  • 2
    @ScottMarlowe is almost certainly right - it is case folding. Your badly designed ORM is double-quoting all your identifiers meaning "DEPARTMENT" is different than "Department" or "department". It is also failing to give you any errors if you have a typo in your identifier names. Turn statement logging on in PostgreSQL if you don't believe him/me. Then, I'm afraid you've got a lot of tedious code search/replace to track down and check all your identifier problems. Commented Apr 26, 2013 at 21:42

2 Answers 2

1

Start psql your_database and issue \d to see the tables.

I'm unfamiliar with Hibernate, but chances are that statements such as @Table(name="DEPARTMENT") are creating tables that are actually called "DEPARTMENT", while your queries are actually querying from DEPARTMENT (without the double quotes), which postgresql will interpret as department.

If so, the fix is to either use lowercase for your table names when creating them, or to make Hibernate use double-quotes and caps when querying your tables.

Sign up to request clarification or add additional context in comments.

Comments

0

If you also have connection issues, the procedure to tackle those is what you would expect.

  1. Check the postgreSQL server is running
  2. Check you can connect via psql/pgAdmin
  3. Check the connection settings in your application are the same as you used in step 2
  4. Check the return codes of your connection call
  5. Turn on connection logging in PostgreSQL and see if you get (a) an attempt and (b) an error
  6. Simplify your code to find out where your error is.

A surprising amount of the time #1 or #3 are the cause, and presumably you aren't doing #4 or you'd not have bothered asking about the other error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.