使用驼峰格式在PostgreSQL中使用主键休眠 [英] Hibernate with Primary Key in PostgreSQL with camelCase Format
问题描述
CREATE TABLEtblChecks
(
checkIdserial NOT NULL,
bankIdtext,
checkNumbertext,
金额数字(30,2)
)
使用hibernate在数据库上创建事务我的问题是
,插入数据。我已经使用以下注释映射了我的
模型
@Entity
@Table(name = \tblChecks \)
public class Check {
/ * Integereger values * /
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =\checkId\)
private Integer checkId;
@Column(名称=金额)
私人双重金额;
$ b $ / *字符串值* /
@Column(name =\bankId \)
private String bankId;
@Column(name =\checkNumber \)
private String checkNumber;
在我的插入方法中发生错误
错误:org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 在此ResultSet中未找到列名checkId。
我的问题是我如何在使用Postgres的主键上修复这些错误。将列名更改为checkid将修复错误,但我无法做到这一点,因为这些表已经与当前系统一起使用。
在POJO中发现的第一个错误是您在PostgresSQL中使用自动增量。这不支持休眠3.x到4.2。因此,您无法在数据库中插入记录,并且可能会收到一个名为 org.hibernate.exception.SQLGrammarException的异常:插入后无法检索生成的标识:
<
您可以使用 serial
或 sequence
来代替。
I have this tables with the following structure
CREATE TABLE "tblChecks"
(
"checkId" serial NOT NULL,
"bankId" text,
"checkNumber" text,
amount numeric(30,2)
)
Im using hibernate to make transactions on the database my problem is with the insertion of the data. I have already mapped my model with the following annotations
@Entity
@Table(name = "\"tblChecks\"")
public class Check {
/* Integereger values*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@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;
In my insertion method there is an error that occurred
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - The column name "checkId" was not found in this ResultSet.
My question is how i can i fix these error on my primary key using Postgres. Changing the column name into checkid will fix the error, but I cant do it because the tables are already in used with the current system.
The First thing wrong I found in your POJO is that you are using Auto Increment in PostgresSQL. This is not support by hibernate 3.x to 4.2. Therefore, you are not able to insert a record in database and may be getting an exception so called org.hibernate.exception.SQLGrammarException: could not retrieve generated id after insert:
Instead of that you can use serial
or sequence
. Serial Doc
Updated POJO class: I have used sequence to generate entity ids and achieved same functionality as of 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;
}
}
Main Code:
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 file: Make sure hibernate.dialect
is 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>
Output:
这篇关于使用驼峰格式在PostgreSQL中使用主键休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!