jpa-2.0

In JPA, having a many-to-one as primary key throws referential integrity constraint violation

你说的曾经没有我的故事 提交于 2019-12-23 03:18:10
问题 I have defined the following entities: @Entity public class Child implements Serializable { @Id @ManyToOne(cascade = CascadeType.ALL) public Parent parent; @Id public int id; } @Entity public class Parent { @Id public int id; } When I try to persist a Child with the following code: Parent p = new Parent(); p.id = 1; Child c1 = new Child(); c1.id = 1; c1.parent = p; em.persist(c1); Hibernate throws a 'Referential integrity constraint violation' error: Caused by: org.h2.jdbc.JdbcSQLException:

JPQL Not a Selected Expression error

六眼飞鱼酱① 提交于 2019-12-23 02:55:27
问题 I have the following JPQL in ProductList Entity class which executes as expected. select DISTINCT new foo.bar.ProductListDTO(p.prodId, " + CASE WHEN (p.prodId = 'ZCX') THEN CONCAT(p.prodDesc, ' - ', e.userId) ELSE p.prodDesc END) from ProductList p LEFT JOIN p.productCatalogueList c where c.userId='ZAM' ProductListDTO class public ProductListDTO(String prodId, String prodDesc, String userId) { this.prodId = prodId; this.prodName = prodDesc; this.userId = userId; } What I would like to achieve

JPA 2.0 CriteriaQuery on tables in @ManyToMany relationship

自闭症网瘾萝莉.ら 提交于 2019-12-22 12:56:13
问题 I have two entities in a @ManyToMany relationship. // Output has 4 other @ManyToOne relationships if that matters @Entity @Table public class Output { @Id public String address; @ManyToMany(targetEntity = Interval.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinTable(name = "output_has_interval", joinColumns = {@JoinColumn(name = "output_address", referencedColumnName = "address")}, inverseJoinColumns = {@JoinColumn(name = "interval_start", referencedColumnName = "start"),

JPA 2.0 CriteriaQuery on tables in @ManyToMany relationship

痴心易碎 提交于 2019-12-22 12:53:48
问题 I have two entities in a @ManyToMany relationship. // Output has 4 other @ManyToOne relationships if that matters @Entity @Table public class Output { @Id public String address; @ManyToMany(targetEntity = Interval.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinTable(name = "output_has_interval", joinColumns = {@JoinColumn(name = "output_address", referencedColumnName = "address")}, inverseJoinColumns = {@JoinColumn(name = "interval_start", referencedColumnName = "start"),

Native query IN clause throwing sql error

扶醉桌前 提交于 2019-12-22 12:07:48
问题 Below query gives me error like Missing IN or OUT parameter at index:: 1 I tried it without brackets in the IN parameters like ... WHERE p.SALES_TYPE IN :types . but still the same error. public List getWithinXDays(ArrayList<String> salesTypes,String sId, String xdays) { Query q = em.createNativeQuery("SELECT p.ORDER_ID FROM formtable p WHERE p.SALES_TYPE IN (:types) AND p.MCODE=:sid AND TRUNC(SYSDATE - p.creationdate) <=:days"); q.setParameter("types",salesTypes); q.setParameter("sid",sId);

JPA update many-to-many deleting records

女生的网名这么多〃 提交于 2019-12-22 11:24:01
问题 I have a @ManyToMany relationship between two entities. When I perform an update on the owning side, it appears that JPA deletes all the linked records from my database and re-inserts them. For me this is a problem because I have a MySQL trigger that fires before a record is deleted. Any ideas on how to get around this problem? @Entity public class User { @Id @Column(name="username") private String username; ... @ManyToMany @JoinTable(name="groups", joinColumns= @JoinColumn(name="username",

Mapping Queue collections in JPA Hibernate

情到浓时终转凉″ 提交于 2019-12-22 10:22:40
问题 Is it possible to have following collection mapping in JPA / hibernate @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}, fetch=FetchType.LAZY,mappedBy="parent") private Deque<Child> childrens; It throws error Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements I am using JPA 2.0 with Hibernate 3 回答1: No, JPA does not support Deque. In JPA 2.0 specification this is explained following way: Collection-valued persistent

JPA : not overriding equals() and hashCode() in the entities?

血红的双手。 提交于 2019-12-22 10:14:35
问题 After reading this article , im bending toward not overriding equals() and hashCode() altogether. In the summary of that article, concerning the no eq/hC at all column, the only consequence is that i couldnt do the comparison operations like : contains() in a List for detached entities, or compare the same entities from different sessions and expect the correct result. But im still in doubt and would like to ask your experiences about this whether it is a bad practice to skip equals and

Is there how I could programmatically ask eclipselink to drop and create all tables?

不羁岁月 提交于 2019-12-22 09:48:08
问题 This helps in unit testing. 回答1: The following should work for you: ServerSession session = entityManager.unwrap(ServerSession.class); SchemaManager schemaManager = new SchemaManager(session); schemaManager.replaceDefaultTables(true, true); 回答2: One way to do that is to execute the sql scripts eclipselink generates specifying: <property name="eclipselink.ddl-generation.output-mode" value="both"/> in persistence.xml 来源: https://stackoverflow.com/questions/3746572/is-there-how-i-could

Is it possible to map a map<String,List<Entity>> in JPA?

本小妞迷上赌 提交于 2019-12-22 08:52:00
问题 I'm thinking if i use maps like Map<String,List<Entity>> or Map<Long,List<Entity>> it will help me with a few things, but I couldn't find any example to map that kind of map. My question is, is it possible to do that kind of mapping in JPA standards, and what kind of annotation I should use? 回答1: Yes, it's possible to create either of those Maps. Why not try it and answer your own question? It would be faster than passively asking here. As for JPA, I would suggest a better abstraction than a