jpa-2.0

Cannot make @ManyToOne relationship nullable

老子叫甜甜 提交于 2019-11-27 20:10:46
I have a many-to-one relationship that I want to be nullable: @ManyToOne(optional = true) @JoinColumn(name = "customer_id", nullable = true) private Customer customer; Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can anyone explain this? Is there a way to make it work? Note that I use JBoss 7, JPA 2.0 with Hibernate as persistence provider and a PostgreSQL 9.1 database. EDIT : I found the cause of my problem. Apparently it is due to the way I defined the primary key in the referenced entity Customer : @Entity @Table public class Customer { @Id @GeneratedValue @Column

CDI injection in EntityListeners

≯℡__Kan透↙ 提交于 2019-11-27 19:20:34
问题 Since JPA 2.0 does not support injection into EntityListener (JPA 2.1 will), decided to use JNDI lookup to get the BeanManager and through it get the logged in user. I defined an EntityListener similar to this: public class MyEntityListener { public static BeanManager getBeanManager() { try { InitialContext initialContext = new InitialContext(); return (BeanManager) initialContext.lookup("java:comp/BeanManager"); } catch (NamingException e) { e.printStackTrace(); return null; } } public

Programmatically loading Entity classes with JPA 2.0?

戏子无情 提交于 2019-11-27 19:09:09
问题 With Hibernate you can load your Entity classes as: sessionFactory = new AnnotationConfiguration() .addPackage("test.animals") .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .addAnnotatedClass(Person.class) .addAnnotatedClass(Dog.class); Is there a way to do the same thing - programmatically loading your Entity classes - in a JPA 2.0 compliant way? The reason for this question is because I'd like to dynamically load my Entity classes, thus not necessarily programmatically. 回答1

Database table access via JPA Vs. EJB in a Web-Application

♀尐吖头ヾ 提交于 2019-11-27 18:52:42
问题 I am designing a web-application that access many database tables. I am trying to figure out what is the preferred way to access those tables? Is it via JPA or EJB? Thanks, Nathan 回答1: The answer is 'both'. EJB itself doesn't access any DB tables. Everything you do in Java that relates to the DB happens via the Java Persistence API (JPA), or if you want to do low level stuff via JDBC but let's not get into that here. What EJB brings to the table is a very easy management of transactions. You

How do I write a MAX query with a where clause in JPA 2.0?

 ̄綄美尐妖づ 提交于 2019-11-27 17:36:26
问题 I'm using JPA 2.0. Hibernate 4.1.0.Final, and Java 6. How do I write a JPA query from the following psuedo-SQL? select max(e.dateProcessed) from Event e where e.org = myOrg And my domain object looks like the following: @GenericGenerator(name = "uuid-strategy", strategy = "org.mainco.subco.core.util.subcoUUIDGenerator") @Entity @Table(name = "sb__event", uniqueConstraints = { @UniqueConstraint(columnNames={"EVENT_ID"}) } ) public class Event { @Id @Column(name = "ID") @GeneratedValue

The mystery of Java EE 6 annotations inheritance

半腔热情 提交于 2019-11-27 16:22:51
问题 I'm using inheritance with EJB in a few scenarios, sometimes with annotations in the super class like this generic entityDAO: public class JpaDAO<T>{ protected Class<T> entityClass; @PersistenceContext(unitName="CarrierPortalPU") protected EntityManager em; protected CriteriaBuilder cb; @PostConstruct private void init() { cb = em.getCriteriaBuilder(); } public JpaDAO(Class<T> type) { entityClass = type; } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void create(T entity) {

Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate

非 Y 不嫁゛ 提交于 2019-11-27 16:06:57
I am a first time user of the new JPA 2.0 Criteria API and I 'm running into a problem when I need to cast a number field to String to compare it with a String parameter. Reason is that I want to search for partial numbers, so I use a 'like' on the CriteriaBuilder. Here's a code sample: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<ParcelDO> cq = cb.createQuery(ParcelDO.class); Root<ParcelDO> parcelDO = cq.from(ParcelDO.class); cq.select(parcelDO); String parcelNumberId = parcelSearchDetailDO.getParcelNumberId(); if (parcelNumberId != null && !parcelNumberId

JPA 2 — Using @ElementCollection in CriteriaQuery

非 Y 不嫁゛ 提交于 2019-11-27 15:14:06
问题 @Entity public class Person { @ElementCollection private List<Location> locations; [...] } @Embeddable public class Location { private Integer dummy; private Date creationDate; [...] } Given the following structure, I'd like to perform the HQL or CriteriaQuery equivalent of the following SQL: SELECT l.* FROM Location l INNER JOIN Person p ON (p.id = l.person_id) WHERE p.id = ? AND l.creationDate > ? I want to get back a list of Locations that are associated with the given person and whose

Persist java.time.Instant (JDK8) with JPA2/Hibernate

点点圈 提交于 2019-11-27 15:03:17
Neither JPA nor Hibernate currently support the new date/time classes brought by JSR-310 in JDK8 (JPA ticket , Hibernate ticket ). Nonetheless, I'd like to code with the JDK8 date/time classes as they are finally well designed. In particular, I'm interested in java.time.Instant , not in full support for all java.time.* types, as all my entities will use this particular class (or so I think now, at least :-) One option is to write a type converter , as defined by JPA 2.1. However, our app server is JBoss EAP 6.3 which is JPA 2.0 but not 2.1 compatible, so this is out of the question for now.

How to get the primary key of any JPA entity?

核能气质少年 提交于 2019-11-27 14:49:02
问题 For every @Entity I need to perform the following: public <Entity> boolean insert(final Entity entity){ if (em.find(entity.getClass(), entity.getId()) == null) { et.begin(); em.persist(entity); et.commit(); return true; } return false; } That is persist the entity if it didn't exist, and know if it did or not exist. With Entity I'm trying to reach @Entity, although I realize it's not an inheritance relationship. What class can I use to refer to every JPA entity? I could just create an