问题描述
我有问题在PostgreSQL数据库中用hibernate创建两个关联的表。我倾向于认为问题在于数据库(配置?),因为我尝试了几乎所有在Internet网站中提出的Hibernate方法,并且我仍然收到相同的错误。使用PostgreSql数据库之前是否需要配置?我无法以其他方式解释出现的错误:
关系部门不存在
我加入两个实体员工和部门如下:
$ p $ @Entity
@Table(name =EMPLOYEE)
public class Employee实现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;
}
私营部门;
和
@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;
}
私人设置<员工>雇员;
我试图用主方法中的几条记录创建表:
SessionFactory sf = new Configuration()。configure()。buildSessionFactory();
会话会话= sf.openSession();
session.beginTransaction();
部门部门=新部门();
department.setDepartmentName(Sales);
session.persist(department);
员工emp1 =新员工(Nina,Mayers,111);
员工emp2 =新员工(Tony,Almeida,222);
emp1.setDepartment(department);
emp2.setDepartment(department);
session.persist(emp1);
session.persist(emp2);
session.getTransaction()。commit();
session.close();
出现错误: session.persist(department);
有人在PostgreSql数据库中有经验,帮助我找出为什么它不能识别表/关系,并且出现错误:
关系部门不存在?
更新:
我查看了日志文件。在Eclipse发出通常的消息之后,消息的最后一行是:由于目标机器主动拒绝,所以无法建立连接。我推断这个问题可能在连接。是否有任何属性,我应该设置(在pgAdmin中)以实现连接?我已经设置了行
< property name =hibernate.connection.url> jdbc:postgresql:// localhost:5432 / Eclipse中的cfg.xml文件中的testDB
c> psql your_database 并发行 \d 来查看表格。I对Hibernate并不熟悉,但有可能是诸如 @Table(name =DEPARTMENT)之类的语句正在创建实际名为的表DEPARTMENT ,而你的查询实际上是从 DEPARTMENT (不带双引号)查询的,postgresql将解释为 department 。
如果是这样,解决方法是在创建表名时使用小写字母,或者使Hibernate使用双引号和大写字母当查询你的表。
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.
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.
这篇关于PostgreSql不会“识别” Hibernate创建的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!