jpa-2.0

JPA criteria query, order on class

余生长醉 提交于 2019-12-09 15:55:46
问题 Is there a way with JPA criteria queries to order on class ? Imagine the following domain objects: abstract class Hobby { ... } class Coding extends Hobby { ... } class Gaming extends Hobby { ... } Using regular QL I was able to do from Hobby h order by h.class But when I apply the same logic on a criteria query, the runtime exception "unknown attribute" occurs. CriteriaQuery<Hobby> criteriaQuery = builder.createQuery(Hobby.class); Root<Hobby> hobbyRoot = criteriaQuery.from(Hobby.class);

JPA CriteriaBuilder case query

ぐ巨炮叔叔 提交于 2019-12-09 13:42:08
问题 Can anyone provide an example of how to write a case query using CriteriaBuilder ? 回答1: What follows is a sample case expression using CriteriaBuilder (this works in JPA 2): Hashtable caseTable = new Hashtable(3); caseTable.put("Bob", "Bobby"); caseTable.put("Susan", "Susie"); caseTable.put("Eldrick", "Tiger"); Expression expression = builder.get("firstName").caseStatement(caseTable, "NoNickname").equal("Bobby"); It generates the following SQL query: "CASE t1.firstName WHEN 'Bob' THEN 'Bobby'

Ehcache Vs Static map cache implementation

随声附和 提交于 2019-12-09 12:41:32
问题 I have few tables with very few entries in them and they will never change dynamically . so i want to cache the whole table in memory to reduce load on DB. I can easily achieve that by a static Map and populating the map in a static block. i was wondering whether the same is possible in a more effective way by Ehcache + hibernate? 回答1: The advantage of a real second-level cache over a static map is that you get the advantage of in-memory access by still keeping the same way of defining,

How do I use CriteriaBuilder to write a left outer join when relationship goes the other way?

拈花ヽ惹草 提交于 2019-12-09 03:13:58
问题 I’m using JPA 2.0, Hibernate 4.1.0.Final and MySQL 5.5.37. I have the following two entities … @Entity @Table(name = "msg") public class Message { @Id @NotNull @GeneratedValue(generator = "uuid-strategy") @Column(name = "ID") private String id; @Column(name = "MESSAGE", columnDefinition="LONGTEXT") private String message; and @Entity @Table(name = "msg_read", uniqueConstraints = { @UniqueConstraint(columnNames = { "MESSAGE_ID", "RECIPIENT" }) }) public class MessageReadDate { @Id @NotNull

Embeddable entity with @OneToMany attribute

我怕爱的太早我们不能终老 提交于 2019-12-09 02:22:12
问题 Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private B b; //getters and setters } @Embeddable public class B { @OneToMany private List<C> cList; //getters and setters } @Entity public class C { @Id @GeneratedValue private Long id; //other fields, getters and setters } Using schema-autogeneration feature with Hibernate I get an additional table which contains mappings between A and C . But I'd like to implement a one-to-many

JPA/Hibernate preUpdate doesn't update parent object

旧城冷巷雨未停 提交于 2019-12-09 00:57:47
问题 In my application I defined following classes: @Entity @Table(name = "forums") public class Forum { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") private String id; private String name; private Date lastActivity; @OneToMany(mappedBy = "forum", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }) private List<Post> posts; @Entity @Table(name = "posts") public class Post { @Id @GeneratedValue(generator = "system

QueryDSL and Play Framework

泪湿孤枕 提交于 2019-12-09 00:18:40
问题 I'm using QueryDSL with JPA2 for some time, and it's the most powerful combination for ORM I know. JPA Criteria API is a disaster. With QueryDSL I've forgotten about JPQL too. I'd like to use QueryDSL with Play! Everything looks very good in Play except those inline parts of JPQL as strings. It reminds me of CakePHP... I'd like to have refactoring-proof querying language in Play (and some other things :) ). QueryDSL usage would be straightforward. It needs EntityManager only. But QueryDSL has

Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT

青春壹個敷衍的年華 提交于 2019-12-08 23:10:05
问题 This post is in continuation of JPA How to get the value from database after persist When I execute the following I am getting following exception, how can I resolve this? Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT DAOImpl code public void create(Project project) { entityManager.persist(project); entityManager.getTransaction().commit(); project = entityManager.find(Project.class, project.getProjectId()); entityManager.refresh(project);

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

痞子三分冷 提交于 2019-12-08 19:04:27
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: Referential integrity constraint violation: "FK3E104FC802AAC0A: PUBLIC.CHILD FOREIGN KEY(PARENT_ID)

Upgrade Glassfish v2 to JPA 2.0?

做~自己de王妃 提交于 2019-12-08 18:26:18
问题 I'm trying to use Hibernate 3.5.5 with Spring HibernateJpaVendorAdapter on Glassfish V2 but I'm getting the following exception when the Spring context is initialised: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode()Ljavax/persistence/SharedCacheMode; at org.hibernate.ejb.util.LogHelper.logPersistenceUnitInfo(LogHelper.java:39) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:517) at org.hibernate.ejb.HibernatePersistence