我的应用程序使用两个表:'用户'和' user_profiles' (DB - PostgreSQL 9.2):
CREATE TABLE "users" (
"id" SERIAL,
"email" varchar(255) NOT NULL UNIQUE,
"password" varchar(255) NOT NULL
PRIMARY KEY("id")
);
CREATE TABLE "user_profiles" (
"id" SERIAL,
"user_id" int4 UNIQUE NOT NULL,
"name" varchar(255) NOT NULL,
"surname" varchar(255) NOT NULL,
"patronymic" varchar(255) NOT NULL,
PRIMARY KEY("id"),
CONSTRAINT "ref_user_profiles_to_users" FOREIGN KEY ("user_id")
REFERENCES "users"("id")
MATCH SIMPLE
ON DELETE RESTRICT
ON UPDATE NO ACTION
NOT DEFERRABLE
);
这是用户的映射代码:
@Entity
@Table( name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"email"})
})
@XmlRootElement
@NamedQueries({
@NamedQuery(
name = "findUserByProviderAndUid",
query = "from Users u where u.provider = :provider AND u.uid =:uid",
fetchSize = 1
)})
public class Users implements Serializable {
private static final long serialVersionUID = 126487584578L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name="id")
private Integer id;
@Email
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(nullable = false, length = 255)
private String email;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(nullable = false, length = 255)
private String password;
@OneToOne(fetch = LAZY, mappedBy = "users")
private UserProfiles userProfile;
//geters and setters
这是UserProfiles的映射代码:
@Entity
@Table(name = "user_profiles", uniqueConstraints = {
@UniqueConstraint(columnNames = {"inn"}),
@UniqueConstraint(columnNames = {"user_id"}),
@UniqueConstraint(columnNames = {"passport_seria", "passport_number"})})
@XmlRootElement
public class UserProfiles implements Serializable {
private static final long serialVersionUID = 12636847237L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name="id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(nullable = false, length = 255)
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(nullable = false, length = 255)
private String surname;
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, unique = true)
@OneToOne(fetch = LAZY)
private Users users;
//getters and setters
每次我选择用户时,我都会收到下一个堆栈跟踪:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.interpretator.kassa.model.UserProfiles.users
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:60)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:76)
at org.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:414)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:255)
at org.hibernate.engine.spi.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:84)
at org.hibernate.engine.spi.EntityUniqueKey.<init>(EntityUniqueKey.java:65)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:755)
at org.hibernate.type.EntityType.resolve(EntityType.java:505)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:324)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2148)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:161)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2385)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:767)
at org.hibernate.type.EntityType.resolve(EntityType.java:505)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
at com.interpretator.kassa.dao.UsersDAOImpl.getUserByProviderAndUid(UsersDAOImpl.java:32)
at com.interpretator.kassa.service.AuthenticationServiceImpl.currentUser(AuthenticationServiceImpl.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy70.currentUser(Unknown Source)
at com.interpretator.kassa.config.filters.TokenAuthenticationFilter.processTokenAuthentication(TokenAuthenticationFilter.java:132)
at com.interpretator.kassa.config.filters.TokenAuthenticationFilter.doFilter(TokenAuthenticationFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: Can not set com.interpretator.kassa.model.Users field com.interpretator.kassa.model.UserProfiles.users to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:379)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57)
... 83 more
这是UsersDAO中的方法:
@Override
public Users getUserByProviderAndUid (String provider, String uid) throws NotFoundException {
Users user = (Users) hibernateTemplate.getSessionFactory().getCurrentSession().getNamedQuery("findUserByProviderAndUid")
.setParameter("provider", provider)
.setParameter("uid", uid)
.uniqueResult();
if (user == null) {
throw new NotFoundException ("Not found user with such provider and uid");
}
return user;
}
更新 - 这里是hql trace
Hibernate:
select
users0_.id as id1_16_,
users0_.account_confirmed_at as account_2_16_,
users0_.account_type as account_3_16_,
users0_.confirmation_token as confirma4_16_,
users0_.count_confirm_phone as count_co5_16_,
users0_.created_at as created_6_16_,
users0_.current_log_in_at as current_7_16_,
users0_.current_sign_in_ip as current_8_16_,
users0_.email as email9_16_,
users0_.email_verified_time as email_v10_16_,
users0_.last_login_time as last_lo11_16_,
users0_.last_sign_in_ip as last_si12_16_,
users0_.locked_at as locked_13_16_,
users0_.login_failed_attempts as login_f14_16_,
users0_.password as passwor15_16_,
users0_.password_confirmed_at as passwor16_16_,
users0_.password_reset_at as passwor17_16_,
users0_.password_reset_token as passwor18_16_,
users0_.phone as phone19_16_,
users0_.phone_code as phone_c20_16_,
users0_.phone_code_confirmed_at as phone_c21_16_,
users0_.phone_verified_time as phone_v22_16_,
users0_.provider as provide23_16_,
users0_.sign_in_count as sign_in24_16_,
users0_.status as status25_16_,
users0_.token as token26_16_,
users0_.uid as uid27_16_,
users0_.unssuccess_login_time as unssucc28_16_,
users0_.updated_at as updated29_16_
from
users users0_
where
users0_.provider=?
and users0_.uid=?
Hibernate:
select
userprofil0_.id as id1_14_0_,
userprofil0_.created_at as created_2_14_0_,
userprofil0_.dob as dob3_14_0_,
userprofil0_.gender as gender4_14_0_,
userprofil0_.inn as inn5_14_0_,
userprofil0_.interests as interest6_14_0_,
userprofil0_.issuer as issuer7_14_0_,
userprofil0_.issuer_date as issuer_d8_14_0_,
userprofil0_.name as name9_14_0_,
userprofil0_.passport_number as passpor10_14_0_,
userprofil0_.passport_seria as passpor11_14_0_,
userprofil0_.patronymic as patrony12_14_0_,
userprofil0_.surname as surname13_14_0_,
userprofil0_.updated_at as updated14_14_0_,
userprofil0_.user_id as user_id15_14_0_
from
user_profiles userprofil0_
where
userprofil0_.user_id=?