jpa-2.0

What to use: JPQL or Criteria API? [closed]

家住魔仙堡 提交于 2019-11-28 15:30:47
My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API? Pascal Thivent I'm pretty sure this has already been covered here on SO but I couldn't find the existing question. So, here is my point of view on the question: I find JPQL queries easier to write/read. I find the Criteria API nice for building dynamic queries. Which is basically what you'll find in Hibernate: Criteria vs. HQL . But there is one

Spring Data-JPA versus JPA: What's the difference?

空扰寡人 提交于 2019-11-28 15:01:26
I am bit confused about the difference between Spring Data-JPA and JPA. I know about JPA that it is a specification for persisting the Java Objects to a relational database using popular ORM technology i.e. In other words JPA provides interfaces and other ORM technologies, implements those interfaces known as JPA provider e.g Hibernate. Now what exactly is Spring Data JPA. Is Spring Data JPA has added some more functionality (Interfaces) over JPA and still it is specified only or it is also a JPA provider? I saw Spring Data JPA works around repositories (DAO layer: if I am not wrong). So I

multiple database support for same JPA classs

喜欢而已 提交于 2019-11-28 14:34:13
We using MYSQL and Hibernate for our project. JPA is used to persist object in DB. We have Multiple class with similar code @Entity @Table(name = "users") class Users implement Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; . . . public Long getId() { return id; } public void setId(Long id) { this.id = id; } } Now we want to give support for oracle too. How should we do it ? strategy=GenerationType.AUTO is not supported by oracle. One soln is we can define sequence in each POJO which we do not want to do? please provide us some input so that we can move ahead

Subquery in From claus in JPA2 Criteria

自作多情 提交于 2019-11-28 14:08:51
I have a data model where an account can have multiple users: class Account { Long id; } class User { Long id; @ManyToOne Account account; } I'd like to do the following query which displays the number of users of each account: select Account.id, NumUsers.num from Account, (select Account.id as account_id, count(User.id) as num from User join Account on User.account_id=Account.id group by Account.id) as NumUsers where Account.id=NumUsers.account_id; I know I can re-write this specific query as: select Account.id, count(User.id) from Account join User on User.account_id=Account.id group by

How to properly cast string to number with JPA2 Criteria API?

北慕城南 提交于 2019-11-28 13:57:10
I am trying to write a query with subselect where a string is cast to a long. I'm probably missing something? Query looks like: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Task> query = cb.createQuery(Task.class); Root<Task> from = query.from(Task.class); Subquery<Long> subquery = query.subquery(Long.class); Root<EntityKeyword> fromKeyword = subquery.from(EntityKeyword.class); subquery.select(fromKeyword.get(EntityKeyword_.relatedToId).as(Long.class)); subquery.where(cb.like(fromKeyword.get(EntityKeyword_.keyword), term)); query.where(cb.in(from.get(ModelEntity_.id)).value

How do I query for only superclass entities in a jpql query?

强颜欢笑 提交于 2019-11-28 12:12:10
I have the following entities: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="orderType", discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue(value="BASE") @Table(name = "orders") public class OrderEntity implements Serializable { ... and @Entity @DiscriminatorValue(value="RECURRING") public class RecurringOrderEntity extends OrderEntity{ ... I can find all the subclasses (RecurringOrderEntity) with the following jpql: Query q = em.createQuery( "SELECT o from RecurringOrderEntity o where " + "o.cancellationDate is null " + "and o

@ManyToMany without join table (legacy database)

我是研究僧i 提交于 2019-11-28 12:07:35
I have to apply JPA in a legacy database with an awful design. Unfortunately is not possible to change it. Luckily is only for read-only access. One of the strangest things I found is a "many-to-many" relationship without a join (or intermediate) table. This is a simplification of the table structure: USER ACCESS ---- ------ ID int primary key ID int primary key NAME varchar2(20) NAME varchar2(20) ACCESS_GROUP int ACCESS_GROUP int ACCESS_GROUP columns can be repeated in both tables One USER can be related to N ACCESS One ACCESS can be related to N USER "Conceptually" this tables must be mapped

Dynamic Named Query in Entity class using JPQL example

时间秒杀一切 提交于 2019-11-28 11:55:57
问题 I have a named query as below; @NamedQuery(name = "MyEntityClass.findSomething", query = "SELECT item FROM MyTable mytbl") Now I want to append dynamic sort clause to this query (based on UI parameters) Can I get an example using JPQL for doing the same (like how to set a dynamic ORDER BY in the Entity class) I have already tried using CriteriaQuery, but was looking for a JPQL implementation now. 回答1: NamedQueries are by definition NOT dynamic, it is not correct to change them

Filter do not initialize EntityManager

ε祈祈猫儿з 提交于 2019-11-28 11:40:28
I trying to use the Open Session in View pattern, but everytime I try to catch the EntityManager in my ManagedBean the entityManager come NULL here is how I'm doing: package filters; // imports.. public class JPAFilter implements Filter { private EntityManagerFactory factory; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { EntityManager entityManager = this.factory.createEntityManager(); request.setAttribute("entityManager", entityManager); entityManager.getTransaction().begin(); chain.doFilter(request,

Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

不问归期 提交于 2019-11-28 10:50:17
I would like to store birthdate so I chose date at MySQL, when I create my entities based in my database, it turns out like this: import java.util.Date; // ..code @NotNull(message="fill you birthdate") @Temporal(TemporalType.DATE) private Date birthdate; But when I try to persist it gives me this error: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details. What am I doing wrong here ? I was reading something about define time zone in Google, I'm from Brazil, how should I do