Tag: jpa

JPA认为我正在删除一个分离的对象

我有一个使用JPA加载和保存域对象的DAO。 我终于设法得到交易的东西工作,现在我有另一个问题。 在我的testing案例中,我打电话给我的DAO加载一个给定id的域对象,检查它是否被加载,然后调用相同的DAO删除刚加载的对象。 当我这样做时,我得到以下内容: java.lang.IllegalArgumentException: Removing a detached instance mil.navy.ndms.conops.common.model.impl.jpa.Group#10 at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:45) at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:108) at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74) at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:794) at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:772) at org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:253) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) at java.lang.reflect.Method.invoke(Method.java:600) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:180) at $Proxy27.remove(Unknown Source) at mil.navy.ndms.conops.common.dao.impl.jpa.GroupDao.delete(GroupDao.java:499) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) at java.lang.reflect.Method.invoke(Method.java:600) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) […]

如何在弹簧容器之外使用Spring Data JPA?

我正在尝试连接Spring Data JPA对象,以便我可以生成DAO代理(又名存储库) – 不使用Spring bean容器。 不可避免的是,我会被问到为什么要这样做:这是因为我们的项目已经在使用Google Guice(并且在使用Gin和GWT的UI上),我们不想维护另一个IoC容器configuration,所有由此产生的依赖关系。 我知道我们可以使用Guice的SpringIntegration ,但这是最后的手段。 看起来,所有东西都可以手动连接对象,但由于没有很好的文档logging,所以我很困难。 根据Spring Data用户指南, 独立使用仓库工厂是可能的。 不幸的是,这个例子显示了RepositoryFactorySupport是一个抽象类。 经过一番search,我设法findJpaRepositoryFactory JpaRepositoryFactory实际上工作得很好,除了它不会自动创build事务。 事务必须手动pipe理,否则什么都不会持久化到数据库: entityManager.getTransaction().begin(); repositoryInstance.save(someJpaObject); entityManager.getTransaction().commit(); 问题原来是@Transactional注释不会自动使用,并需要一个TransactionInterceptor 值得庆幸的是,在返回之前, JpaRepositoryFactory可以进行callback以将更多的AOPbuild议添加到生成的Repository代理中: final JpaTransactionManager xactManager = new JpaTransactionManager(emf); final JpaRepositoryFactory factory = new JpaRepositoryFactory(emf.createEntityManager()); factory.addRepositoryProxyPostProcessor(new RepositoryProxyPostProcessor() { @Override public void postProcess(ProxyFactory factory) { factory.addAdvice(new TransactionInterceptor(xactManager, new AnnotationTransactionAttributeSource())); } }); 这是事情做得不好的地方。 通过代码中的debugging器, TransactionInterceptor确实创build了一个事务 – […]

使用JPA时@Entity和@Table中的名称参数有什么区别?

我使用的是JPA2,@Entity和@Table都有一个name属性,例如: @Entity(name="Foo") @Table (name="Bar") class Baz 我应该使用什么,哪些是可选的? 在我的具体情况下,我有一个类的User和一个类Group ,它有额外的要求(据我所知),因为它们是在SQL中的保留字。 一个工作的解决scheme是怎么样的,在写查询时我会用哪个名字来引用这个实体呢? 更新。 我在Group为两个注释添加了name="GROUPS" ,对User也做了相同的处理,但现在我得到这个错误: Exception Description: The table [USERS] is not present in this descriptor. Descriptor: RelationalDescriptor(example.Group –> [DatabaseTable(GROUPS)]) 和 Internal Exception: java.sql.SQLException: Table not found in statement [SELECT ID, MAXIMUMROLE, MEMBERSHIPS_ID FROM USERS]

JPA:实现模型层次结构 – @MappedSuperclass与@Inheritence

我正在使用PostgreSQL和JPA Play Framework 1.2.4。 我想有一个模型层次结构,并看到有这样做的一些替代scheme。 我有一个基类(这是抽象的)和两个扩展这个基类的具体类。 我不想坚持这个基础课,而我想要具体的课。 在基类中,我有另一个Model类作为属性,换句话说,我在我的基类中有@ManyToOne关系。 我的问题是实现这个的最好方法是什么? 使用@MappedSuperclass或@Inheritence与TABLE_PER_CLASS策略? 我有点困惑,因为它们看起来几乎相同。 我对于将来可能面临的查询和性能问题也有一些担忧。

我应该在JPA实体中编写equals()方法吗?

我想检查实体是否在另一个实体的集合成员( @OneToMany或@ManyToMany )中: if (entity2.getEntities1().contains(entity1)) { }

JPA – 坚持一对多的关系

也许这是一个愚蠢的问题,但它正在扰乱我。 我有一个双向的一对多员工对车辆的关系。 当我第一次坚持雇员在数据库(即它没有分配的ID),我也希望其相关的车辆坚持。 这对我来说很好,除了我保存的Vehicle实体没有自动映射关联的Employee,而在数据库中,Vehicle表中的employee_id外键列为空。 我的问题是,是否有可能让车辆的员工在员工持续的同时坚持? 我意识到,雇员将需要先保存,然后车辆保存之后。 JPA能自动为我做这个吗? 或者我必须做如下的事情: Vehicle vehicle1 = new Vehicle(); Set<Vehicle> vehicles = new HashSet<Vehicle>(); vehicles.add(vehicle1); Employee newEmployee = new Employee("matt"); newEmployee.setVehicles(vehicles); Employee savedEmployee = employeeDao.persistOrMerge(newEmployee); vehicle1.setAssociatedEmployee(savedEmployee); vehicleDao.persistOrMerge(vehicle1); 谢谢! 编辑:按要求,这是我的映射(没有所有其他方法等) @Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="employee_id") private Long id; @OneToMany(mappedBy="associatedEmployee", cascade=CascadeType.ALL) private Set<Vehicle> vehicles; … } @Entity public […]

如何使用JPA2的@Cacheable而不是Hibernate的@Cache

通常,我使用Hibernate的@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)来caching一个@Entity类,它运行良好。 在JPA2中,还有另外一个@Cacheable注释,看起来和Hibernate的@Cache有相同的function。 为了让我的实体类独立于Hibernate的包,我想试试看。 但是我不能使它工作。 每次简单的ID查询仍然击中数据库。 谁能告诉我哪里出了问题? 谢谢。 实体类: @Entity //@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Cacheable(true) public class User implements Serializable { // properties } testing课: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:app.xml"}) @TransactionConfiguration(transactionManager="transactionManager") public class UserCacheTest { @Inject protected UserDao userDao; @Transactional @Test public void testGet1() { assertNotNull(userDao.get(2L)); } @Transactional @Test public void testGet2() { assertNotNull(userDao.get(2L)); } @Transactional @Test public […]

JTA,JPA和Plain JDBC在hibernate方面的区别

在hibernate方面,JTA,JPA和Plain JDBC有什么区别?

为什么要在Spring Data JPA Repository上的save()之后使用返回的实例?

这里是代码: @Repository public interface AccountRepository extends JpaRepository<Account, Long> {} 来自Spring Data JPA项目的JpaRepository 。 这里是testing代码: public class JpaAccountRepositoryTest extends JpaRepositoryTest { @Inject private AccountRepository accountRepository; @Inject private Account account; @Test @Transactional public void createAccount() { Account returnedAccount = accountRepository.save(account); System.out.printf("account ID is %d and for returned account ID is %d\n", account.getId(), returnedAccount.getId()); } } 结果如下: account […]

Eclipse不能识别persistence.xml的内容

Im在eclipse中得到以下错误: persistence.xml文件没有识别的内容。 我的persistence.xml文件在我的应用程序中效果很好,但eclipse不断给我这个错误。 移动文件并使用m2eclipse更新我的项目configuration后,我得到了这个。 我没有改变文件本身。 有人知道如何解决这个问题? persistence.xml中: <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="localDB" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>package.Users</class> <properties> <!– enable warnings for debugging –> <property name="openjpa.Log" value="DefaultLevel=TRACE, Runtime=INFO, Tool=INFO, SQL=TRACE"/> <!– connection properties –> <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost/test"/> <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="openjpa.ConnectionUserName" value="root"/> <property name="openjpa.ConnectionPassword" value=""/> </properties> </persistence-unit> </persistence> 更新 看起来像m2eclipse与jpa结合的bug。 […]