jpa-2.0

JPA Criteria select all instances with max values in their groups

微笑、不失礼 提交于 2019-11-28 10:24:18
Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from Season s1 where s1.end = ( select max(s2.end) from Season s2 where s1.contest=s2.contest ) This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity. EntityManger em; //to be injected or constructed CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Season> cq = cb.createQuery(Season

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

旧街凉风 提交于 2019-11-28 10:13:34
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 many-to-one to this class via a corresponding foreign key. I would like the owned class to have a

Many to Many hibernate inverse side ignored

一曲冷凌霜 提交于 2019-11-28 10:06:49
Hi am reading the hibernate documentation. http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html A many-to-many association is defined logically using the @ManyToMany annotation. You also have to describe the association table and the join conditions using the @JoinTable annotation. If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be ignored when updating the relationship values in the association table): I understand everything but the last (ie. it will be ignored when updating the relationship values

getting error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2

半世苍凉 提交于 2019-11-28 09:47:49
I am spring spring 3.2. Here is my config file <bean id="legacyDataSource" name="legacydb" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true"> <property name="driverClassName" value="${jdbc.legacy.driverClassName}" /> <property name="url" value="${jdbc.legacy.url}" /> <property name="username" value="${jdbc.legacy.username}" /> <property name="password" value="${jdbc.legacy.password}" /> </bean> <bean id="ls360DataSource" name="Ls360db" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" > <property name="driverClassName" value

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

删除回忆录丶 提交于 2019-11-28 09:15:38
问题 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

FetchMode in JPA 2 CriteriaQuery

可紊 提交于 2019-11-28 09:13:55
I'm currently in the process of switching from Hibernate to pure JPA 2 (which by the way turned out to be much more time consuming than I initially expected). The biggest problem I'm having so far is finding a way to force eager loading of lazy properties. With Hibernate this was done using: criteria.setFetchMode("person", FetchMode.JOIN); . Is there any way to do this with JPA 2? Try this: CriteriaQuery<Person> c = cb.createQuery(Person.class); Root<Person> person = c.from(Person.class); person.fetch("address"); c.select(person); Assuming there is a one-to-one relationship between your Person

Exception in GWT Dev Mode + Spring 3.1 + Hibernate 4.0.1

纵饮孤独 提交于 2019-11-28 08:38:16
I have a GWT+Hibernate+JPA+Spring configuration file which is OK with Spring 3.0.x + Hibernate 3.6.x. When I upgraded to Spring 3.1 and Hibernate 4.0, the following exception is thrown: Is there any known incompatibility withe Hibernate 4 and Spring 3.1? org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/application-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: myPu] Unable to build EntityManagerFactory at org

How to run an aggregate function like SUM on two columns in JPA and display their results?

痞子三分冷 提交于 2019-11-28 08:07:15
I am new to JPA. So my question should be so simple to some. Below is the Simple Query in SQL which i would like to convert to JPA. I already have an entity class called TimeEnt . SELECT SUM(TimeEntryActualHours) as UnBilledHrs, SUM (TimeEntryAmount) as UnbilledAmount FROM TimeEnt WHERE MatterID = 200 The JPA Query Language does support aggregates functions in the SELECT clause like AVG, COUNT, MAX, MIN, SUM and does support multiple select_expressions in the SELECT clause, in which case the result is a List of Object array ( Object[] ). From the JPA specification: 4.8.1 Result Type of the

Generated column and table names in hibernate with underscore

柔情痞子 提交于 2019-11-28 07:57:06
问题 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"/> <!-- ... --

Recursive JPA query?

北慕城南 提交于 2019-11-28 07:35:55
Does JPA 2 have any mechanism for running recursive queries? Here's my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I'd like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to do this in a single query? I'm using Hibernate 3.5.3, but I'd prefer not to have any explicit dependencies on Hibernate APIs. EDIT: According to this item, Hibernate does not have this feature, or at least it didn't in March. So it seems unlikely that JPA would