jpa-2.0

@OrderColumn, @OneToMany & null index column for collection

久未见 提交于 2019-11-30 07:06:25
I am trying to create parent child tables where the order is preserved. The example 7.8 from Hibernate documentation shows how to do this: @Entity public class Customer { @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } private Integer id; @OneToMany(mappedBy="customer") @OrderColumn(name="orders_index") public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } private List<Order> orders; } @Entity public class Order { @Id @GeneratedValue public Integer getId() { return id; }

How to do bulk delete in JPA when using Element Collections?

两盒软妹~` 提交于 2019-11-30 06:48:11
I am having trouble working out how to do a bulk delete of a Person object using JPA, when the Person objects contain data stored using an @ElementCollection . Any ideas on how to do this would be much appreciated. @Entity @Table(name="at_person") public class Person implements Comparable<Person> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id") private long id = 0; @Column(name="name", nullable = true, length = 128) private String name = ""; @ElementCollection @Column(name = "email") @CollectionTable(name = "person_email", joinColumns = @JoinColumn(name = "person_id"))

Multiple writable mappings exception in EclipseLink

雨燕双飞 提交于 2019-11-30 05:58:47
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-directional one-to-one association to Company @OneToOne(mappedBy="user", cascade={CascadeType.ALL})

JPQL statement returning boolean value

我们两清 提交于 2019-11-30 05:41:59
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 in JPQL, that would return boolean value, or is it only possible in Java code? Yes, it is possible with

How JPA transactions works

喜夏-厌秋 提交于 2019-11-30 05:25:51
Following code gets executed whenever I want to persist any entity. Things seems to be working fine but I fail to understand how it works ! EntityManager em = getEntityManager(); EntityTransaction userTransaction = em.getTransaction(); userTransaction.begin(); em.persist( ent ); userTransaction.commit(); The EntityManager above is a single instance shared by whole application. After starting the transaction; I just say em.persist(entity).. How does hibernate know it belongs to which transaction ! Suppose there are 10 concurrent users on my application and all 10 threads executing above code.

Difference Hibernate 3.5 / JPA 2.0

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 05:02:56
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's your opinion? What's left in Hibernate 3.5 that I can't do with JPA 2.0 ? Pascal Thivent Now, with

jpa criteria for many to many relationship

天涯浪子 提交于 2019-11-30 04:52:26
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 to find the collaborators for an answer given by id. I have already done this with Hibernate Criteria (

Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT

自作多情 提交于 2019-11-30 04:28:30
This post is in continuation of JPA How to get the value from database after persist When I execute the following I am getting following exception, how can I resolve this? Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT DAOImpl code public void create(Project project) { entityManager.persist(project); entityManager.getTransaction().commit(); project = entityManager.find(Project.class, project.getProjectId()); entityManager.refresh(project); System.out.println("Id -- " + project.getProjectId()); System.out.println("no -- " + project.getProjectNo())

How to check a collection size in JPA2

时光毁灭记忆、已成空白 提交于 2019-11-30 04:09:07
问题 Consider the following: @Entity public class Book { private List<String> authors; @ElementCollection public List<String> getAuthors() { return authors; } public void setAuthors(List<String> authors) { this.authors = authors; } } How to type a JPA2 CriteriaQuery expression which, say, will let me find all the Books which have more than 2 authors? 回答1: In JPQL: select b from Book where size(b.authors) >= 2 Using the criteria API (but why would you replace such a simple static query with the

Correct usage of JPA criteria API, Predicates and where method of CriteriaQuery

寵の児 提交于 2019-11-30 04:03:12
I am trying to test a JPA repository. Here is my client/test code: @Test public void testFindBoitesByMultiFieldWithIdentifiantSet() { BoiteDataOnDemand dod = new BoiteDataOnDemand(); Boite boite = dod.getSpecificBoite(0); boite.setIdentifiant("theIdentifiant"); boiteRepository.save(boite); assertEquals(10, Boite.countBoites()); BoiteQueryInfo boiteQueryInfo = new BoiteQueryInfo(); boiteQueryInfo.setIdentifiant("theIdentifiant"); List<Boite> boites = boiteRepository.findBoitesByMultiField(boiteQueryInfo, 1, 5, "identifiant", "desc"); assertEquals(1, boites.size()); } Here is the repository