jpa-2.0

How do I catch the constraint violation exception from EclipseLink?

白昼怎懂夜的黑 提交于 2019-12-04 00:59:13
I am using EclipseLink in my web application, and I am having a hard time gracefully catching and handling Exceptions it generates. I see from this thread what seems to be a similar problem, but I don't see how to work around or fix it. My code looks like this: public void persist(Category category) { try { utx.begin(); em.persist(category); utx.commit(); } catch (RollbackException ex) { // Log something } catch (HeuristicMixedException ex) { // Log something } catch (HeuristicRollbackException ex) { // Log something } catch (SecurityException ex) { // Log something } catch

JPA 2: multiple column usage in foreign keys

匆匆过客 提交于 2019-12-03 23:35:02
I am using Hibernate as persistence provider and modelling my entities with JPA 2. Now a question came up and i hope you can help me. In my application you can open up a Game, create groups of players in it and walk around on the map (tiles (2d)). First my entity definitions: Game: @Entity public class Game implements Serializable { @Id @SequenceGenerator(name = "gen_gameid", sequenceName = "seq_gameid") @GeneratedValue(generator="gen_gameid") private long gameid; /** * Playing Characters. */ @OneToMany(mappedBy = "game") private List<Character> characters; private int round = 0; @OneToMany

Better Exception Handling in JPA

夙愿已清 提交于 2019-12-03 23:15:21
I used EJB3/JPA when persisting my entities and I am happy on how it is able to manage my DB related task. My only concern is on the exception handling. My sample code when saving entity always comes in this flavor. Most of the tutorials that I read on the net comes in this flavor also with no regards to exception handling. @Stateless public class StudentFacade{ @PersistenceContext(unitName = "MyDBPU") private EntityManager em; public void save(Student student) { em.persist(student); } } But I dont know whats the best way of exception handling in an EJB app? What should be the best way when

JPA CriteriaBuilder case query

浪子不回头ぞ 提交于 2019-12-03 20:13:07
Can anyone provide an example of how to write a case query using CriteriaBuilder ? 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' WHEN 'Susan' THEN 'Susie' WHEN 'Eldrick' THEN 'Tiger' ELSE 'NoNickname' END = 'Bobby'" For more info please

JPA concat operator

ε祈祈猫儿з 提交于 2019-12-03 19:43:58
问题 Is there a JPA concat operator for string concatenation? I know there is a JPA CONCAT function, however its ugly to use for concatenating multiple strings. CONCAT(CONCAT(CONCAT(cola,colb),colc),cold) Vendors like Oracle offer || some other like Microsoft offer + . Is there a standard JPA concatenation operator so that I could create a query like cola || colb || colc || cold I tried + using openjpa with SQL Server, however it seems to be invalid JPQL. I couldn't find anything regarding such an

ignorecase in criteria builder in JPA

帅比萌擦擦* 提交于 2019-12-03 18:51:33
问题 How can we do a ignorecase in the criteria builder? If I have private final CriteriaBuilder cb then I can only use cb.asc or cb.desc , but not ignoring case. 回答1: How can we do a ignorecase in the criteria builder 1. Force Ignorecase in JPA Program - Does the Job, Answers the Q Directly For a one-arg operation (i.e. ORDER BY), convert argument to lowercase (or uppercase). For a two-arg operation (e.g. = or LIKE or ORDER BY), convert both args to LC (or UC). JPA ORDER BY Two Columns, Ignoring

Class not eligible for getting processed by all BeanPostProcessors

怎甘沉沦 提交于 2019-12-03 18:40:02
问题 I'm having a lot of trouble to set up a configuration of Spring + Spring Data JPA + QueryDSL + JPA 2.0 + Hibernate in Maven. I already solved a lot of problems, but this one is giving me headache =/. I'm getting the following exception: Bean 'dataSource' of type [class org.springframework.jdbc.datasource.DriverManagerDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) I took a look in Google and most of times this problem

How to map a MySQL char(n) column to an instance variable using a JPA/Hibernate annotation?

拟墨画扇 提交于 2019-12-03 18:01:06
问题 I encounter a JPA/Hibernate mapping problem on a column "language" in a MySQL table whose type is char(7). In my entity, the code generated for the field is: private String language; this causes the following exception at runtime: ... 43 more Caused by: javax.persistence.PersistenceException: [PersistenceUnit: prosvetaPersistenceUnit] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911) at org.hibernate.ejb

JPA and Hibernate proxy behavior

南楼画角 提交于 2019-12-03 16:38:57
i tried to observe JPA2 / Hibernate4 proxy behavior below, // Circular entity with lazy loading: @Entity public class Employee { @Id@Generated int id; String name; @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) Employee boss; public String toString() { return id + "|" + name + "|" + boss; } //getters and setters ... } // Persist entities: // Outer entity: Employee employee = new Employee(); employee.setName("engineer"); // Inner entity: Employee boss = new Employee(); boss.setName("manager"); employee.setBoss(boss); entityTransaction.begin(); entityManager.persist(employee);

Cross database joins in JPA

眉间皱痕 提交于 2019-12-03 16:27:40
Is it possible to do cross database table joins in JPA? I have a users table in one database which has a foreign key to a organizations table in a separate database. Both the databases are on same physical machine. Now MySQL allows me to write queries which span across multiple databases, but I am not sure how to do this with JPA. The @Entity annotations on the Java POJO's don't take the name of the database so there is no way to mark a cross DB relationship. Is there a workaround for this situation? Perhaps using a native query to load the joined entity? We tried out the following approach