我正在使用hibernate Jersey和Spring在java上构建RESTful服务,并意识到我的实体之间的关系应该不包括类的实例,只包括标识符(外键)。但是在尝试查询实体时我有例外。我有以下实体:
@Entity
@javax.persistence.Table(name = "manager_user")
public class ManagerUser extends User {
@ManyToOne(targetEntity = ShopAdminUser.class)
private Integer shopAdminUserId;
//...
}
@Entity
@javax.persistence.Table(name = "shop_admin_user")
public class ShopAdminUser extends User {
@Lob
private String contactData;
public String getContactData() {
return contactData;
}
public void setContactData(String contactData) {
this.contactData = contactData;
}
}
@Entity
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class User {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
private String firstName;
private String lastName;
@Temporal(TemporalType.DATE)
private Date dob;
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String password;
@Lob
private String personalData;
@OneToOne(targetEntity = Photo.class)
private Integer photoId;
//...getters and setters
}
我在下一个方法中遇到异常:
@Transactional(readOnly = true)
public List<ManagerUser> getByShopAdminUserID(Integer id) {
Session session = sessionFactory.getCurrentSession();
List managerUsers = session.createQuery(
"from ManagerUser m where m.shopAdminUserId = :shopAdminUserId")
.setParameter("shopAdminUserId", id).list();
return managerUsers;
}
堆栈追踪:
Mar 28, 2015 4:10:56 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.foodservice.businesslogic.user.ShopAdminUser.id
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:60)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:293)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:616)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1901)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doList(Loader.java:2554)
at org.hibernate.loader.Loader.doList(Loader.java:2540)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
at org.hibernate.loader.Loader.list(Loader.java:2365)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at com.foodservice.dao.ManagerUserDAO.getByShopAdminUserID(ManagerUserDAO.java:50)
at com.foodservice.dao.ManagerUserDAO$$FastClassBySpringCGLIB$$7447621a.invoke(<generated>)
...
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.foodservice.businesslogic.user.User.id to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57)
... 120 more
修改
我的SQL由Hibernate提供
Hibernate:
select
manageruse0_.id as id1_0_,
manageruse0_.dob as dob2_0_,
manageruse0_.email as email3_0_,
manageruse0_.firstName as firstNam4_0_,
manageruse0_.gender as gender5_0_,
manageruse0_.lastName as lastName6_0_,
manageruse0_.password as password7_0_,
manageruse0_.personalData as personal8_0_,
manageruse0_.photoId_id as photoId11_0_,
manageruse0_.systemStatus as systemSt9_0_,
manageruse0_.userType as userTyp10_0_,
manageruse0_.shopAdminUserId_id as shopAdmi2_6_,
manageruse0_.state as state1_6_
from
manager_user manageruse0_
where
manageruse0_.shopAdminUserId_id=?
Hibernate:
select
shopadminu0_.id as id1_0_0_,
shopadminu0_.dob as dob2_0_0_,
shopadminu0_.email as email3_0_0_,
shopadminu0_.firstName as firstNam4_0_0_,
shopadminu0_.gender as gender5_0_0_,
shopadminu0_.lastName as lastName6_0_0_,
shopadminu0_.password as password7_0_0_,
shopadminu0_.personalData as personal8_0_0_,
shopadminu0_.photoId_id as photoId11_0_0_,
shopadminu0_.systemStatus as systemSt9_0_0_,
shopadminu0_.userType as userTyp10_0_0_,
shopadminu0_.contactData as contactD1_16_0_,
photo1_.id as id1_9_1_,
photo1_.image as image2_9_1_,
photo1_.name as name3_9_1_
from
shop_admin_user shopadminu0_
left outer join
photo photo1_
on shopadminu0_.photoId_id=photo1_.id
where
shopadminu0_.id=?
我做错了什么?帮帮我)
答案 0 :(得分:1)
我有一个似乎有问题。现在我解决了它。这很简单。 你的代码 - &gt;
@ManyToOne(targetEntity = ShopAdminUser.class)
private Integer shopAdminUserId;
应该是
@ManyToOne(targetEntity = ShopAdminUser.class)
@JoinColumn(name="shopAdminUserId" references...=""(i forget how to spell))
private ShopAdminUser shopAdminUser;
这样做:
@OneToOne(targetEntity = Photo.class)
private Integer photoId;
应该是
@OneToOne(targetEntity = Photo.class)
@JoinColumn(name="photoId" references...=""(i forget how to spell))
private Photo photo;
因为hibernate会自动将类转换为int或其他东西
答案 1 :(得分:0)
尝试在类中没有参数构造函数(ShopAdmin User and Manager User)。 Hibernate使用它来实例化实体。