Tag: jpa

JPA找到最后一个条目

我想知道用JPA获取表的最后一个条目的最佳方法是什么。 在Sql中,我正在寻找的是: select id from table order by id desc limit 1 我在考虑使用model.count(),但这听起来更像是一个黑客而不是一个好的解决方案;)

JPA CascadeType.PERSIST如何工作?

在我的示例中, Employee与Department with CascadeType.PERSIST具有OneToOne关系。 当我坚持多个Employee , 为什么EntityManager为所有Employee记录保留单个Department记录? 我的期望是,如果我们使用CascadeType.PERSIST ,当持久化Employee时,将为每个Employee记录重新创建一个Department记录。 Employee.java @Entity public class Employee { private String id; private String name; @OneToOne(cascade = CascadeType.PERSIST) @JoinColumn(name = “DEP_ID”, referencedColumnName = “ID”) private Department department; —– } Department.java @Entity public class Department implements Serializable { private String id; private String name; } Test.java public void insert() { […]

Spring boot + Hibernate + JPA没有事务性的EntityManager可用

我使用Spring引导1.2.3.RELEASE版本与JPA over hibernate。 我遇到以下exception org.springframework.dao.InvalidDataAccessApiUsageException: No transactional EntityManager available; nested exception is javax.persistence.TransactionRequiredException: No transactional EntityManager available at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:410) ~[EntityManagerFactoryUtils.class:4.1.6.RELEASE] at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:223) ~[HibernateJpaDialect.class:4.1.6.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[AbstractEntityManagerFactoryBean.class:4.1.6.RELEASE] at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[ChainedPersistenceExceptionTranslator.class:4.1.6.RELEASE] at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[DataAccessUtils.class:4.1.6.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[PersistenceExceptionTranslationInterceptor.class:4.1.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [ReflectiveMethodInvocation.class:4.1.6.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122) ~[CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.class:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [ReflectiveMethodInvocation.class:4.1.6.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [ExposeInvocationInterceptor.class:4.1.6.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [ReflectiveMethodInvocation.class:4.1.6.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [JdkDynamicAopProxy.class:4.1.6.RELEASE] at com.sun.proxy.$Proxy110.deleteByCustomerId(Unknown […]

JPA选择查询与where子句

我想编写一个select语句,但无法弄清楚如何编写where子句… 我的代码: CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery(); query.select(query.from(SecureMessage.class)).where(); 这是我传递字符串的方法。 我想只获取与Im传递给方法的字符串值匹配的行。

使用JPA EntityManager加入两个不相关的表

当两者之间没有FK / PK关系时,我需要在属性上加入两个JPA实体。 我正在使用Hibernate,可以像这样使用HQL查询 select foo, bar from FooEntity as foo, BarEntity as bar where foo.someothercol = ‘foo’ and foo.somecol = bar.somecol 但是,我想避免依赖Hibernate并使用EntityManager。 请帮忙。

Oracle将空字符串视为Java / JPA程序员的NULL问题

如何处理Oracle在数据库中将空字符串存储为null的情况? 我希望它存储为空字符串,因为它不是NULL,因为发出查询会更容易。 像这样的东西会选择空字符串和非空字符串,但不会选择空值 select * from mytable where myfield like ‘%’; 如果我还要选择空值(原本应该是空字符串),我必须选择如下: select * from mytable where myfield like ‘%’ or myfield is null; 我希望在我的sql语句中or myfield is null跳过做or myfield is null 我想到的当前解决方案是在应用程序级别中处理这个问题,例如,在实体中,我将我的所有String字段默认值初始化为空格,例如: @Entity public class MyEntity { private String name = ” “; public void setName(String name) { if (isEmptyString(name)) { name = ” “; […]

应用程序与容器管理的EntityManager

我目前在理解JPA的概念时遇到了问题。 我目前正在使用/开发最近的EclipseLink,Glassfish,Derby数据库来演示一个项目。 在我开发更大的图片之前,我需要绝对确定这个PersistingUnit如何在不同的范围内工作。 我有一堆servlet 3.0,目前在request.session对象中保存用户的相关实体类(同一个war文件中的所有内容)。 我目前正在使用EntityManagerFactory和UserTransaction注入的应用程序管理的EntityManager。 它在我自己测试时运行顺畅。 当2个人同时访问相同的实体时,会发生不同版本的实体。 我希望使用托管bean跨越相同的WAR,如果可能的话,使用相同的持久性单元。 我已经阅读了http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html以及那些对我来说根本没有意义的范围的解释。 长话短说 ,应用程序和容器管理的EntityManagers的用途和区别是什么?

JPA Eclipselink数据库更改通知不会使缓存条目无效

我有两个使用Eclipselink 2.4.2进行持久化的Java 1.7应用程序。 一个应用程序是在Glassfish 3.1.2.2中运行的JEE应用程序,另一个是Java SE应用程序。 这些应用程序读取和写入相同的数据,因此可能存在过时的JPA缓存条目。 我试图使用Oracle DCN来解决过时的缓存问题。 我已经按照上面的链接中的描述配置了我的persistence.xml: org.eclipse.persistence.jpa.PersistenceProvider jdbc/DRMS 然而,我仍然得到陈旧的缓存条目。 如果一个应用程序提交对数据库的更改,则第二个应用程序仍会看到其旧的过时缓存条目,而不是新的更改。 使用调试器,我已经确认OracleChangeNotificationListener正在接收数据库事件,但它似乎永远不会使缓存中的任何内容无效。 Eclipselink OracleChangeNotificationListener注册以下侦听器以接收Oracle数据库更改事件: public void onDatabaseChangeNotification(DatabaseChangeEvent changeEvent) { databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, “dcn_change_event”, changeEvent); if (changeEvent.getTableChangeDescription() != null) { for (TableChangeDescription tableChange : changeEvent.getTableChangeDescription()) { ClassDescriptor descriptor = OracleChangeNotificationListener.this.descriptorsByTable.get(new DatabaseTable(tableChange.getTableName())); if (descriptor != null) { CacheIndex index = descriptor.getCachePolicy().getCacheIndex(fields); for (RowChangeDescription rowChange : […]

Hibernate:无法反序列化 – 无效的流标头

有关如何解决此错误的任何想法? 我正在使用Spring JPA和Hibernate。 以下必要的详细信息。 实体类1: @Entity @Table(name = “ways”) @TypeDef(name = “hstore”, typeClass = HstoreUserType.class) @Cacheable public class Way { /** * Primary key for the row in table. */ @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = “id”) private Long id; /** * The ID to represent it across the system. * Used for preserving historical […]

是否有充分的理由使用XML而不是通过注释配置hibernate?

我一直在使用Hibernate几年,但只使用它注释,并在我的代码中设置连接参数。 我没有使用XML文件“遗漏了什么”? 是否只有XML可用的重要function? 是否存在使用XML有意义的情况或模式?