jpa-2.0

JPA 2.0 : what is javax.validation.* package?

假如想象 提交于 2019-12-18 03:56:32
问题 What is the javax.validation package in Java EE? How to use this with JPA? I want to validate my JPA Entity with the @NotNull annotation. My JPA implementation is Hibernate. Here's my Entity: public class Employee implements Serializable , EmployeeDetail { private static final long serialVersionUID = 1L; @Id @GeneratedValue private int id; private String name; private long salary; ... 回答1: Using the Bean Validation Api you can quite easy ensure standard validation (not null, patterns, email)

JPA Entity - Specify Persistence Unit?

北城以北 提交于 2019-12-18 02:49:07
问题 I have a JavaEE project that makes use of multiple persistence units. Is there any way to specify which persistence unit a particular JPA Entity belongs to? Some entities are in one data source, while others are in my second data source. Is there a way to differentiate between the two using annotations? 回答1: To specify which persistent unit an Entity belongs to, use the persistence.xml file: <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org

JPA: defining an index column [duplicate]

青春壹個敷衍的年華 提交于 2019-12-18 02:49:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Specifying an index (non unique key) using JPA Is there a way to define index on enitity column, to improve searches performance? I saw that hibernate gives @Index and @IndexColumn, but I am looking for JPA way to do it. thanks Here is an example of my entity, I need to index a name column @Entity @Table(name = "MY_TABLE") public class MyEntity { private long id; private String name; private String sourceInfo; .

JPA: defining an index column [duplicate]

瘦欲@ 提交于 2019-12-18 02:49:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Specifying an index (non unique key) using JPA Is there a way to define index on enitity column, to improve searches performance? I saw that hibernate gives @Index and @IndexColumn, but I am looking for JPA way to do it. thanks Here is an example of my entity, I need to index a name column @Entity @Table(name = "MY_TABLE") public class MyEntity { private long id; private String name; private String sourceInfo; .

How do I count the number of rows returned by subquery?

强颜欢笑 提交于 2019-12-18 02:27:42
问题 I want to do something like this: select count(*) from (select ...) (As it would be in SQL), but in JPA. Any ideas on how I would do it? 回答1: This should do the trick (If you want to use JPA criteria API): CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> query = cb.createQuery(Long.class); Root<Entity> root = query.from(Entity.class); //Selecting the count query.select(cb.count(root)); //Create your search criteria Criteria criteria = ... //Adding search

JPA's cascade = REMOVE and Hibernate's @OnDelete used together?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 01:19:11
问题 I have inherited a code base on which nearly all relations have the following annotations: @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "someThing") @OnDelete(action = OnDeleteAction.CASCADE) Now I'm having trouble understanding what @OnDelete does in the first place. Hibernate: OnDelete vs cascade=CascadeType.REMOVE is interesting, but unfortunately doesn't have any answers and the JavaDoc for @OnDelete is particularly worthless. From the other questions it

JPA, Entity manager, select many columns and get result list custom objects

不问归期 提交于 2019-12-17 22:34:32
问题 How I can get list of custom objects, like results below query: SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id By example: return getEntityManager().createQuery("SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id").setParameter("id", id).getResultList(); I need a map with category id and number of products in category. 回答1: Unfortunately, JPA doesn't provide a standard way to

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

旧街凉风 提交于 2019-12-17 21:24:18
问题 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

multiple database support for same JPA classs

让人想犯罪 __ 提交于 2019-12-17 21:16:19
问题 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

How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?

巧了我就是萌 提交于 2019-12-17 19:22:06
问题 I'm asking and answering my own question, but i'm not assuming i have the best answer. If you have a better one, please post it! Related questions: How to set a backreference from an @EmbeddedId in JPA hibernate mapping where embeddedid (?) JPA Compound key with @EmbeddedId I have a pair of classes which are in a simple aggregation relationship: any instance of one owns some number of instances of the other. The owning class has some sort of primary key of its own, and the owned class has a