Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
<hibernate.version>3.2.6.ga</hibernate.version>
<hibernate.entitymanager.version>3.3.2.GA</hibernate.entitymanager.version>
Mapping documents:
package zeuzgroup.domain.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.domdrides.entity.UuidEntity;
@Entity
public class City extends UuidEntity {
private static final long serialVersionUID = 1L;
private Location location = new Location();
public void setLocation(Location location) {
this.location = location;
}
private List<Event> events = new ArrayList<Event>();
private Country country;
@ManyToOne
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@OneToMany(cascade = { CascadeType.ALL })
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
@Embedded
@AttributeOverride(name = "name", column = @Column(name = "locationName"))
public Location getLocation() {
return location;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getName();
}
}
package zeuzgroup.domain.entity;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.domdrides.entity.UuidEntity;
import org.wicketopia.annotation.metadata.EditorType;
import org.wicketopia.annotation.metadata.Order;
import org.wicketopia.annotation.validator.Length;
import org.wicketopia.annotation.validator.Required;
@Entity
public class Event extends UuidEntity {
private static final long serialVersionUID = 1L;
@Enumerated
private CategoryType categoryType;
@ManyToOne(cascade = { CascadeType.ALL })
private City city;
@Temporal(TemporalType.DATE)
private Date dateEnd = Calendar.getInstance().getTime();
@Temporal(TemporalType.DATE)
private Date dateStart = Calendar.getInstance().getTime();
private String description;
/**
* Estimated size of event
*/
private int estimatedSize;
@Enumerated
private EventAdmittanceType eventAdmittanceType;
@Enumerated
private EventType eventType;
@Embedded
private ImageEntry image;
private List<Link> links = new ArrayList<Link>();
private Location location = new Location();
private String name;
private String originalTags;
private List<Tag> tags = new ArrayList<Tag>();
public CategoryType getCategoryType() {
return categoryType;
}
public City getCity() {
return city;
}
public Date getDateEnd() {
return dateEnd;
}
public Date getDateStart() {
return dateStart;
}
@Column(length = 1024)
public String getDescription() {
return description;
}
public int getEstimatedSize() {
return estimatedSize;
}
public EventAdmittanceType getEventAdmittanceType() {
return eventAdmittanceType;
}
public EventType getEventType() {
return eventType;
}
public ImageEntry getImage() {
return image;
}
@OneToMany(cascade = { CascadeType.ALL })
public List<Link> getLinks() {
return links;
}
@ManyToOne(cascade = { CascadeType.ALL })
public Location getLocation() {
return location;
}
@Order(1)
@EditorType("long-string")
@Required
@Length(min = 25)
@Column(length = 64)
public String getName() {
return name;
}
public String getOriginalTags() {
return originalTags;
}
@ManyToMany(cascade = { CascadeType.ALL })
public List<Tag> getTags() {
return tags;
}
public void setCategoryType(CategoryType categoryType) {
this.categoryType = categoryType;
}
public void setCity(City city) {
if (this.city != null) {
this.city.getEvents().remove(this);
}
this.city = city;
if (city != null) {
city.getEvents().add(this);
}
}
public void setDateEnd(Date dateEnd) {
this.dateEnd = dateEnd;
}
public void setDateStart(Date dateStart) {
this.dateStart = dateStart;
}
public void setDescription(String description) {
this.description = description;
}
public void setEstimatedSize(int estimatedSize) {
this.estimatedSize = estimatedSize;
}
public void setEventAdmittanceType(EventAdmittanceType eventAdmittanceType) {
this.eventAdmittanceType = eventAdmittanceType;
}
public void setEventType(EventType eventType) {
this.eventType = eventType;
}
public void setImage(ImageEntry image) {
this.image = image;
}
public void setLinks(List<Link> links) {
this.links = links;
}
public void setLocation(Location location) {
this.location = location;
}
public void setName(String name) {
this.name = name;
}
public void setOriginalTags(String originalTags) {
this.originalTags = originalTags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
}
Code between sessionFactory.openSession() and session.close():
Im using spring jpa template:
getJpaTemplate().find(jpql);
Full stack trace of any exception that occurs:
(Only happens with the IN select statement)
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:310)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.orm.jpa.JpaTemplate$CloseSuppressingInvocationHandler.invoke(JpaTemplate.java:404)
at $Proxy30.createQuery(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$9.doInJpa(JpaTemplate.java:313)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:184)
at org.springframework.orm.jpa.JpaTemplate.executeFind(JpaTemplate.java:151)
at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:311)
at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:307)
at zeuzgroup.domain.repository.jpa.JpaEventRepository.searchForEvents(JpaEventRepository.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy26.searchForEvents(Unknown Source)
at zeuzgroup.domain.EventTest.testSearchForEventAllEventsInFrederikssund(EventTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:71)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:175)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:283)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:254)
at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:172)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:255)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:
<hsqldb.version>1.8.0.7</hsqldb.version>
The generated SQL (show_sql=true):
Hibernate: select city0_.id as id6_, city0_.country_id as country8_6_, city0_.countryName as countryN2_6_, city0_.latitude as latitude6_, city0_.longitude as longitude6_, city0_.locationName as location5_6_, city0_.nearestCityName as nearestC6_6_, city0_.name as name6_ from City city0_ where city0_.name=? and city0_.country_id=?
And
select event0_.id as id0_, event0_.categoryType as category2_0_, event0_.city as city0_, event0_.dateEnd as dateEnd0_, event0_.dateStart as dateStart0_, event0_.description as descript6_0_, event0_.estimatedSize as estimate7_0_, event0_.eventAdmittanceType as eventAdm8_0_, event0_.eventType as eventType0_, event0_.lastModified as lastMod10_0_, event0_.picture as picture0_, event0_.location_id as location14_0_, event0_.name as name0_, event0_.originalTags as origina13_0_ from Event event0_ where event0_.id='a4bb750b-fe50-471d-ba85-33b31dac7a18'
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html
So basicly im just trying to select Event's from a City. I've tried the two approaches below, none works.
String jpql = "select e from Event e, IN ( e.city ) c WHERE c.id='"+ city.getId()+"'";
String jpql = "select e from Event e WHERE e.city.id='" + city.getId()+"'";