jpa-2.0

JPA Hibernate many-to-many cascading

旧巷老猫 提交于 2019-11-29 19:59:03
I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows: public class User implements Serializable { @Id @Column(name="USER_ID") private String userId; @ManyToMany @JoinTable(name = "USER_GROUP", joinColumns = { @JoinColumn(name = "GROUP_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") } ) private Set<Group> groupList; //get set methods } public class Group { @Id @Column(name="GROUP_ID") private String groupId; @ManyToMany(mappedBy="groupList") private Set<User> memberList; //get set methods } And then, I create a user and group and then assign the user

Which Java Type do you use for JPA collections and why?

五迷三道 提交于 2019-11-29 19:57:20
Which of the following collection types do you use in your JPA domain model and why: java.util.Collection java.util.List java.util.Set I was wondering whether there are some ground rules for this. UPDATE I know the difference between a Set and a List . A List allows duplicates and has an order and a Set cannot contain duplicate elements and does not define order. I'm asking this question in the context of JPA. If you strictly follow the definition, then you should always end up using the Set type, since your collection is stored in relational database, where you can't have duplicates and where

JPA 2 and Hibernate 3.5.1 MEMBER OF query doesnt work

自闭症网瘾萝莉.ら 提交于 2019-11-29 19:07:56
问题 I'm trying the following JPQL and it fails misserably: Query query = em.createQuery("SELECT u FROM User u WHERE 'admin' MEMBER OF u.roles"); List users = query.query.getResultList(); I get the following exception: ERROR [main] PARSER.error(454) | <AST>:0:0: unexpected end of subtree java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree [SELECT u FROM com.online.data.User u WHERE 'admin' MEMBER OF u.roles] ERROR [main] PARSER.error(454) |

JPA Self Join using JoinTable

主宰稳场 提交于 2019-11-29 17:35:36
I have 1 entity call Item in which I want to be able to link parent items to children. to use a join table to create a parent/child relationship. I haven't been able to get any good documentation on. So if anyone has any thoughts I'm all ears. Here is what I have... which works most of the time. public class Item implements java.io.Serializable { @Id private Long id; @ManyToOne(optional = true, fetch = FetchType.LAZY) @JoinTable(name = "ITEMTOITEM", joinColumns = { @JoinColumn(name = "ITEMID") }, inverseJoinColumns = { @JoinColumn(name = "PARENTITEMID") } ) private Item parent; @OneToMany

JPA Criteria Predicate Conditions

巧了我就是萌 提交于 2019-11-29 15:48:27
问题 I have the following code snippet for building criteria builder where condition. Would like to know are there any ways of making this better as I would have more where conditions and same conditions will be used for getting count of records. Any insight is highly appreciable private List <Product> getProducts(MultivaluedMap params) throws JSONException { CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery <Product> criteriaQuery = criteriaBuilder

JPA @JoinColumn annotation with One To One relationship

无人久伴 提交于 2019-11-29 15:30:46
Is it true that @JoinColumn can be used on both side of One to One relationship in JPA ? I was under impression that it should be always used in owning side of One to One relationship as owning side will have foreign key column and this annotation define the attribute of foreign key column . Please clarify if my understanding is not correct . Edit #1 - I wanted to know , In which scenario we will be using @JoinColumn annotation on both side of one to one relationship ? The OneToOne relationship is not necessarily bi-directional. A bi-directional OneToOne relationship happens when a reference

How to find by date from timestamp column in JPA criteria?

牧云@^-^@ 提交于 2019-11-29 15:28:58
I want to find a record by date. In entity and database table, datatype is timestamp. I used Oracle database. @Entity public class Request implements Serializable { @Id private String id; @Version private long version; @Temporal(TemporalType.TIMESTAMP) @Column(name = "CREATION_DATE") private Date creationDate; public Request() { } public Request(String id, Date creationDate) { setId(id); setCreationDate(creationDate); } public String getId() { return id; } public void setId(String id) { this.id = id; } public long getVersion() { return version; } public void setVersion(long version) { this

Generated column and table names in hibernate with underscore

本秂侑毒 提交于 2019-11-29 14:13:07
how do I force hibernate to generate db schema such that it converts CamelCase into Underscores (using HBM)? Eg. I have: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cz.csas.pdb.be.model.product.passive"> <class name="foo.BarBaz"> <id name="barBazId"> <generator class="sequence"/> </id> <property name="extractContactType"/> <!-- ... --> </class> </hibernate-mapping> And I want hibernate to create table like this (oracle): CREATE TABLE

JPA with JTA: Persist entity and merge cascaded child entities

吃可爱长大的小学妹 提交于 2019-11-29 13:24:14
I have a bidirectional one-to-many relationship with the following entity classes: 0 or 1 client <-> 0 or more product orders When persisting the client entity I want the associated product order entities to be persisted, too (as their foreign key to the "parent" client may have been updated). Of course all required CASCADE options are set on the client side. But it does not work if a newly created client is persisted for the first time while referencing an existing product order as in this scenario: product order '1' is created and persisted. Works fine. client '2' is created and product

JPA: Store a list of integers in a single field

点点圈 提交于 2019-11-29 13:09:46
Is it possible to store a list of integers in a single field of the respective entity table with standard JPA 2? Something like: @Entity @Table(name="tbl_myentities") public class MyEntity { @ElementaryCollection @Column(name="vals") // in table tbl_myentities private List<Integer> vals; Thanks MrKiane It is not possible to store multiple values in a single field. Whats the reason behind storing them in a single field? A way could be to use a field of type String and add all integers there in a comma separated list and join/explode in getters and setters: private String vals; public setVals