Tag: hibernate

HIbernate commit()和flush()

我GOOGLE了很多,并了解org.hibernate.Transaction.commit()和org.hibernate.Session.flush()很多,知道每个方法的目的,但仍然有一个问题。 手工调用org.hibernate.Session.flush()方法是不是很好? 正如org.hibernate.Session文档中所说, 必须在工作单元结束时调用,在提交事务并closures会话之前(取决于flush-mode,Transaction.commit()调用此方法)。 你能解释一下,如果org.hibernate.Transaction.commit()会自动调用org.hibernate.Session.flush()的目的吗? 谢谢!

澄清术语:“保湿”实体:从数据库获取属性

在实体的ORM /惰性加载的情况下,我对“水合”一词的理解如下: “保湿”描述了填充使用延迟加载获取的实体的一些或全部先前未填充的属性的过程。 例如:class Author从数据库加载: @Entity class Author { @Id long id; List<Book> books; } 最初, bookscollections不填充。 我的理解是,从数据库加载books集合的过程被称为“保湿”集合。 这个定义是否正确,是常用的术语? 在这个过程中还有另一个更常见的术语吗?

javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional

我不明白注释javax.transaction.Transactional和org.springframework.transaction.annotation.Transactional之间的实际区别是什么? org.springframework.transaction.annotation.Transactional是javax.transaction.Transactional的扩展还是完全不同的意思? 什么时候应该每个人都使用? 服务层中的Spring @Transactinal和DAO中的javax ? 谢谢回答。

将PostgreSQL 9.2.1连接到Hibernate

我有一个空白的Spring MVC项目,我使用Maven安装了Hibernate和PostgreSQL驱动程序。 我在完整的教程上显示了如何连接PostgreSQL和Hibernate。 任何帮助吗?

通过注释使用Hibernate UUIDGenerator

我正在使用我的uuid如下: @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") @Column(name = "uuid", unique = true) private String uuid; 但我得到一个聪明的hibernate警告: 使用org.hibernate.id.UUIDHexGenerator,它不生成符合IETF RFC 4122标准的UUID值; 请考虑使用org.hibernate.id.UUIDGenerator 所以我想切换到org.hibernate.id.UUIDGenerator ,现在我的问题是如何告诉它到Hibernate的生成器。 我看到一些人用它作为“hibernate-uuid” – 所以这就是我所尝试的,但有负面的结果: @Id @GeneratedValue(generator = "hibernate-uuid") @GenericGenerator(name = "hibernate-uuid", strategy = "hibernate-uuid") @Column(name = "uuid", unique = true) private String uuid;

事务标记为仅回滚:如何find原因

我在使用@Transactional方法提交事务时遇到问题: methodA() { methodB() } @Transactional methodB() { … em.persist(); … em.flush(); log("OK"); } 当我从methodA()调用methodB()时,方法成功通过,我可以在日志中看到“OK”。 但是,然后我得到 Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) […]

hibernate标准限制和/或组合

我将如何实现这个使用hibernate限制? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z')))

我如何创build一个使用hibernate注释的外键约束?

我正在尝试使用hibernate注释为我的数据库表编写模型类。 我有两个表,每个都有一个主键用户和问题。 @Entity @Table(name="USER") public class User { @Id @Column(name="user_id") @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(name="username") private String username; // getter and setter } 问题表。 @Entity @Table(name="QUESTION") public class Questions extends BaseEntity{ @Id @Column(name="question_id") @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name="question_text") private String question_text; // getter and setter } 而且我还有一个表UserAnswer,它具有userId和questionId作为上述两个表中的外键。 但我无法find如何在UserAnswer表中引用这些约束。 @Entity @Table(name="UserAnswer ") public class UserAnswer { […]

Hibernate:如何用HQL设置NULL查询参数值?

我怎么能设置一个hibernate参数为“空”? 例: Query query = getSession().createQuery("from CountryDTO c where c.status = :status and c.type =:type") .setParameter("status", status, Hibernate.STRING) .setParameter("type", type, Hibernate.STRING); 在我的情况下,状态string可以为空。 我debugging了这个和hibernate,然后生成一个像这样的SQLstring/查询…. status = null …这然而不能在MYSQL中工作,因为正确的SQL语句必须是“ status is null ”(Mysql不明白status = null,并将其评估为false,以便根据我读过的mysql文档,查询将不会返回任何logging…) 我的问题: 为什么Hibernate没有正确地将空string转换为“空”(而不是错误地创build“= null”)? 什么是重写这个查询,以便它是无效的最好的方法是什么? 用nullsafe我的意思是说,在“状态”string为空的情况下,应该创build一个“为空”? 非常感谢你! 蒂姆

将Hibernate Query.list()转换为List <Type>的“正确”方法是什么?

我是Hibernate的新手,我正在写一个简单的方法来返回匹配特定filter的对象列表。 List<Foo>似乎是一个自然的返回types。 无论我做什么,除非我使用丑陋的@SuppressWarnings ,否则我似乎无法使编译器高兴。 import java.util.List; import org.hibernate.Query; import org.hibernate.Session; public class Foo { public Session acquireSession() { // All DB opening, connection etc. removed, // since the problem is in compilation, not at runtime. return null; } @SuppressWarnings("unchecked") /* <—– */ public List<Foo> activeObjects() { Session s = acquireSession(); Query q = s.createQuery("from foo […]