使用 hibernate-spring 在 postgres 数据库中插入记录。序列生成器问题
我试图在 postgres 数据库中插入一条记录,但插入失败。
当尝试选择数据时,选择工作正常,所以我认为它不是 spring-hibernate 配置错误
环境:Spring 3.1,Hibernate 3.6,Postgres 9.1
这是我的代码:
实体类:
@Entity
public class Person implements Serializable {
@Id
@Column(insertable=false, updatable=false)
@Type(type="java.lang.Long")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PERSON_SEQ")
@SequenceGenerator(name="PERSON_SEQ", sequenceName="PERSON_SEQ", allocationSize=1)
private Long id;
@Column
private String firstName;
}
My Dao:
@Repository
public class PersonDao extends BaseDao
public void insertPerson(){
super.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Person p = new Person();
p.setFirstName("george");
session.persist(p);
return null;
}
});
}
}
第一次部署应用程序时,在 postgres 中创建一个新序列
CREATE SEQUENCE person_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE person_seq
OWNER TO postgres;
sql 插入期间的 hibernate sql 输出是:
Hibernate:
select
nextval ('PERSON_SEQ')
Hibernate:
insert
into
Person
(firstName, lastName, money, id)
values
(?, ?, ?, ?)
但它永远不会插入记录,即使序列递增1
Im trying to insert a record in a postgres db but insertion fails.
When trying to select data, selection works fine, so i suppose its not a spring-hibernate configuration error
environment: Spring 3.1, Hibernate 3.6, Postgres 9.1
here is my code :
Entity class:
@Entity
public class Person implements Serializable {
@Id
@Column(insertable=false, updatable=false)
@Type(type="java.lang.Long")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PERSON_SEQ")
@SequenceGenerator(name="PERSON_SEQ", sequenceName="PERSON_SEQ", allocationSize=1)
private Long id;
@Column
private String firstName;
}
My Dao:
@Repository
public class PersonDao extends BaseDao
public void insertPerson(){
super.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Person p = new Person();
p.setFirstName("george");
session.persist(p);
return null;
}
});
}
}
At the first time the application is deployed, a new sequence is created in postgres
CREATE SEQUENCE person_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE person_seq
OWNER TO postgres;
The hibernate sql output during sql insert is:
Hibernate:
select
nextval ('PERSON_SEQ')
Hibernate:
insert
into
Person
(firstName, lastName, money, id)
values
(?, ?, ?, ?)
but it never inserts the record, even if the sequence increments by 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来事务从未提交。
您是否使用 Spring 配置了事务管理器,或者在调用
insertPerson
的服务上定义了 @Transactional?It looks like the transaction is never committed.
Have you configured your transaction manager with Spring or defined @Transactional on your service that calls
insertPerson
?