jpa-2.0

JPA2 Criteria-API: select… in (select from where)

懵懂的女人 提交于 2019-11-29 05:17:17
I have the following database model: A aId AB aId bId B bId status In a Spring data Specification, I want to return the instances of A when B.status is 'X'. The JPQL code is the following: select a from A a where a in (select ab.id.a from AB ab where ab.id.b.status= :status) These are the model classes: @Entity public class A { private Long aId; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.a") private Set<AB> ab; } @Entity public class B { private Long bId; private String Status; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.b")

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

泄露秘密 提交于 2019-11-29 05:09:10
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 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 always need those with JPA, and it's a bit of pain to manage these manually. EJB also gives you very easy

JPQL statement returning boolean value

老子叫甜甜 提交于 2019-11-29 04:42:21
问题 Is it possible to write JPQL query like following: select count(*) > 0 from Scenario scen where scen.name = :name that would return true/false boolean values depending of whether entity filling criteria exists or not? I would like to use the query this way: boolean exists = entityManager.createQuery(query,Boolean.class).setParameter("name",name).getSingleResult(); The query from my example just isn't syntactically correct (parse error), but is there any correct way of doing checks like that

How to create specification using JpaSpecificationExecutor by combining tables?

﹥>﹥吖頭↗ 提交于 2019-11-29 04:36:40
I am using JpaSpecificationExecutor for creating custom queries. How do I create a Specification for the following SQL? select * from employee e, address a where e.id=23415 and e.name="Deepak" and a.city="Leeds"; Java Class : public static Specification<Employee> searchEmployee(final Map<String,String> myMap) { return new Specification<Employee>(){ @Override public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) { //Need to query two tables Employee and Address } } Here is a test that works @Test public void test1() { repository.save(makeEmployee("billy",

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

岁酱吖の 提交于 2019-11-29 03:28:20
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(generator = "uuid-strategy") private String id; @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType

Difference Hibernate 3.5 / JPA 2.0

孤街醉人 提交于 2019-11-29 02:49:03
问题 So far, I always prefered to use Hibernate directly rather than JPA 1.0, because JPA was lacking some of the important features I needed and Hibernate provided: Criteria API, second level cache, unidirectional OneToMany and a few others. Now, with the advent of JPA 2.0 and all the new features that come with it and that were initially missing in JPA 1.0 (http://en.wikibooks.org/wiki/Java_Persistence/What_is_new_in_JPA_2.0%3F), I wonder if there is still a need to use Hibernate directly. What

jpa criteria for many to many relationship

血红的双手。 提交于 2019-11-29 02:23:22
问题 I have 2 POJO classes in Java, Answer and Collaborator, in a many-to-many relationship. class Answer { @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "ANSWERS_COLLABORATORS", joinColumns = { @JoinColumn(name = "aid") }, inverseJoinColumns = { @JoinColumn(name = "cid") }) private Set<Collaborator> collaborators = new HashSet<Collaborator>(0); } Class Answer has a set of Collaborator , but a Collaborator doesn't keep a set of Answer . What I need to do from Hibernate CriteriaQuery is

Row numbering with p:dataTable

半世苍凉 提交于 2019-11-29 02:22:18
I have this query: SELECT @rownum:=@rownum+1 'no', m.title, m.author, REPLACE(SUBSTRING_INDEX(m.content, ' ', 20), '<br>', ' '), m.viewed, m.hashid FROM book m, (SELECT @rownum:=0) r WHERE m.lang = ?1 AND m.title like CONCAT('%',?2,'%') ORDER BY m.title asc The @rownum:=@rownum+1 part of the MySQL query for result numbering as Primefaces currently does not have a facility to display a numbering column. Is there a way to show Primefaces column numbering without having to do @rownum:=@rownum+1 ? If not, can I construct the above query using purely the CriteriaBuilder method? I am not quite sure

The mystery of Java EE 6 annotations inheritance

五迷三道 提交于 2019-11-29 02:03:20
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) { em.persist(entity); } @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public T find

Multiple writable mappings exception in EclipseLink

主宰稳场 提交于 2019-11-29 01:49:00
问题 I have these tables: Which my intention is : A user can be a company or a person but each one of them have something in common, as username which is the email and password , so I used the JPA Tools to generate the entities from the table which result on this: public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String email; private String password; private int reputation; //bi