我有以下结构的表格
[[Call]]
我使用hibernate在数据库上进行事务我的问题是 随着数据的插入。我已经映射了我的 具有以下注释的模型
CREATE TABLE "tblChecks"
(
"checkId" serial NOT NULL,
"bankId" text,
"checkNumber" text,
amount numeric(30,2)
)
在我的插入方法中出现错误
错误:org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 在此ResultSet中找不到列名“checkId”。
我的问题是如何使用Postgres在主键上修复这些错误。将列名更改为checkid将修复错误,但我不能这样做,因为这些表已经与当前系统一起使用。
答案 0 :(得分:0)
我在你的POJO中发现的第一件事是你在PostgresSQL中使用自动增量。这不是hibernate 3.x到4.2的支持。因此,您无法在数据库中插入记录,并且可能会收到所谓的org.hibernate.exception.SQLGrammarException: could not retrieve generated id after insert:
的异常
您可以使用serial
或sequence
代替。 Serial Doc
更新了POJO类:我使用序列生成实体ID并实现了与auto_increment相同的功能。
package stack.filter;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "\"tblChecks\"")
public class Check implements Serializable
{
private static final long serialVersionUID = 1L;
/* Integereger values */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="checkId_seq")
@SequenceGenerator(name="checkId_seq", sequenceName="checkId_seq", allocationSize=1)
@Column(name = "\"checkId\"")
private Integer checkId;
@Column(name = "amount")
private Double amount;
/* String values */
@Column(name = "\"bankId\"")
private String bankId;
@Column(name = "\"checkNumber\"")
private String checkNumber;
public Integer getCheckId()
{
return checkId;
}
public void setCheckId(Integer checkId)
{
this.checkId = checkId;
}
public Double getAmount()
{
return amount;
}
public void setAmount(Double amount)
{
this.amount = amount;
}
public String getBankId()
{
return bankId;
}
public void setBankId(String bankId)
{
this.bankId = bankId;
}
public String getCheckNumber()
{
return checkNumber;
}
public void setCheckNumber(String checkNumber)
{
this.checkNumber = checkNumber;
}
}
主要代码:
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
Check c1=new Check();
c1.setAmount(40555.4);
c1.setBankId("AC11112");
c1.setCheckNumber("CK12222CD");
Check c2=new Check();
c2.setAmount(50555.4);
c2.setBankId("AC11111");
c2.setCheckNumber("CK12233EW");
session.persist(c1);//Insert check object c1
session.persist(c2);//Insert check object c2
tx.commit();
System.out.println("After commit");
} catch (RuntimeException e) {
try {
tx.rollback();
} catch (RuntimeException rbe) {
System.err.println("Couldn’t roll back transaction"+ rbe);
}
throw e;
} finally{
if(session != null){
session.close();
}
}
System.out.println("Success");
}
hibernate.cfg.xml文件:确保hibernate.dialect
为org.hibernate.dialect.PostgreSQLDialect
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property><!-- com.mysql.jdbc.Driver -->
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres?createDatabaseIfNotExist=true</property> <!-- jdbc:mysql://localhost:5432/postgres?createDatabaseIfNotExist=true -->
<property name="hibernate.connection.username">postgres</property><!-- postgres -->
<property name="hibernate.connection.password">postgres</property><!-- postgres -->
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="stack.filter.Check"/>
</session-factory>
<强>输出:强>