I am very new in postgresql and hybernate and i am getting a problem in my first program, can you please help me out and thanks in advance.
我对postgresql和hybernate非常陌生,在我的第一个程序中我遇到了一个问题,你能帮我一下吗?
It prints comment
它打印评论
Hibernate:
/* insert testhybernate1.Employee
*/ insert
into
employee1
(ename, mobile, email, id)
values
(?, ?, ?, ?)
and gives error:-
并给出错误:-
org.postgresql.util.PSQLException: ERROR: relation "employee1" does not exist
I added
我添加了
<property name="hibernate.hbm2ddl.auto">create</property>
in my config file then that problem solved a new table created by hybernate with name Employee. But whatever i am inserting in this table through my program it is not updating in my database. First question is why by adding
在我的配置文件中,这个问题解决了由hybernate和name Employee创建的新表。但是无论我通过程序在这个表中插入什么它都不会在数据库中更新。第一个问题是为什么要加
<property name="hibernate.hbm2ddl.auto">create</property>
my problem solved and second question is why it is not updating in my database when i search through pgadmin?
我的问题和第二个问题是为什么当我搜索pgadmin时,它没有在我的数据库中更新?
In the database postgesql in public schema, i created a table employee1 with all column and table name in small letter.my mapping file "empmapping.hbm.xml"
在公共模式的数据库postgesql中,我创建了一个表employee1,所有列和表名都用小写字母表示。我的映射文件“empmapping.hbm.xml”
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="testhybernate1.Employee" table="employee1">
<id name="id" column="id" type="integer">
<generator class="assigned"></generator>
</id>
<property name="ename" column="ename" type="string"></property>
<property name="mobile" column="mobile" type="long"></property>
<property name="email" column="email" type="string"></property>
</class>
</hibernate-mapping>
My config file "hypernate.cfg.xml" is given below:-
我的配置文件”hypernate.cfg。下面:xml”
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql:template1</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">Monu26@dmail</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="empmapping.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
my pogo class:- Employee.java
我的pogo类:——Employee.java
package testhybernate1;
import java.io.Serializable;
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String ename;
private long mobile;
private String email;
public Employee(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
My application class Hybernate1.java
我的应用程序类Hybernate1.java
package testhybernate1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Hybernate1 {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
System.out.println("starting........");
Configuration cfg= new Configuration();
// cfg.configure();
try
{
cfg.configure();
}
catch(Exception e)
{
System.out.println("*** Exception while configuring:"+e);
}
SessionFactory sf=null;
try
{
// SessionFactory sf=new SessionFactory(cfg);
sf=cfg.buildSessionFactory();
}
catch(Exception e)
{
System.out.println("exception in creating sessionfactory :"+e);
}
if(sf.equals(null))
return;
else
{
Session s=sf.openSession();
Transaction tx = s.beginTransaction();
Employee emp=new Employee();
emp.setId(1);
emp.setEname("Raghav");
emp.setMobile(95899);
emp.setEmail("k123@mail");
Integer eid=(Integer)s.save(emp);
//s.flush();
tx.commit();
s.close();
System.out.println("emp id in database="+eid);
}
// TODO Auto-generated method stub
}
}
1 个解决方案
#1
2
Sorry for the bug,
抱歉错误,
I got the answer, In my config file there should be
我得到了答案,在配置文件中应该有
<property name="hibernate.connection.url">jdbc:postgresql:postgres</property>
instead of
而不是
<property name="hibernate.connection.url">jdbc:postgresql:template1</property>
#1
2
Sorry for the bug,
抱歉错误,
I got the answer, In my config file there should be
我得到了答案,在配置文件中应该有
<property name="hibernate.connection.url">jdbc:postgresql:postgres</property>
instead of
而不是
<property name="hibernate.connection.url">jdbc:postgresql:template1</property>