一切正常,直到我尝试映射实体类,UserAccount和Profile之间的关系。 UserAccount具有主键“id”,并且Profile具有主键“nickname”,它是UserAccount的外键。 当我尝试登录时发生异常。当使用不存在的用户名和密码时,不会发生此异常。 省略了不相关的字段。
@Entity
@Table(name="users", uniqueConstraints={ @UniqueConstraint( columnNames=
{"user_id", "username", "nickname", "email"} ) } )
public class UserAccount implements Serializable {
private static final long serialVersionUID = -7400604230107519063L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="user_id", nullable=false, unique=true, length=12)
private Integer id;
@Column(name="username", nullable=false, unique=true, length=25)
private String username;
@Column(name="nickname", nullable=false, unique=true, length=25)
private String nickname;
@OneToOne(mappedBy="userAccount")
@Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private Profile profile;
@Entity
@Table(name="profiles", uniqueConstraints={ @UniqueConstraint(
columnNames={"nickname"} ) } )
public class Profile implements Serializable {
private static final long serialVersionUID = -6888548766932228312L;
@Id
@Column(name="nickname", nullable=false, unique=true, length=25)
private String nickname;
@OneToOne
@JoinColumn(name="nickname")
private UserAccount userAccount;
public UserAccount getUserAccount(String username)
{
Session session = this.sessionFactory.getCurrentSession();
String hql = "from UserAccount U where U.username =:user_username";
@SuppressWarnings("unchecked")
Query<UserAccount> query =
session.createQuery(hql).setParameter("user_username",username);
List<UserAccount> result = (List<UserAccount>)query.list(); //exception
return result.get(0);
}
Caused by: java.lang.IllegalArgumentException:
org.hibernate.TypeMismatchException: Provided id of the wrong type for
class com.app.web.social.model.Profile. Expected: class
java.lang.String, got class java.lang.Integer
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1420)
at com.app.web.social.dao.UserDAOImpl.getUserAccount(UserDAOImpl.java:69)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy50.getUserAccount(Unknown Source)
at com.app.web.social.service.SocialWebAppUserDetailsService.loadUserByUsername(SocialWebAppUserDetailsService.java:29)
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114)
... 42 more
Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.app.web.social.model.Profile. Expected: class java.lang.String, got class java.lang.Integer
at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEventListener.java:166)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:646)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:165)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:125)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1152)
at org.hibernate.loader.Loader.processResultSet(Loader.java:1011)
at org.hibernate.loader.Loader.doQuery(Loader.java:949)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2692)
at org.hibernate.loader.Loader.doList(Loader.java:2675)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
at org.hibernate.loader.Loader.list(Loader.java:2502)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:384)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1490)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
答案 0 :(得分:0)
如果没有进一步的细节,我将推测这是因为Profile的@Id列'昵称'是数据库表上的整数。
Profile表的id是否定义为Integer?
因为'nickname'在@Entity代码中声明为String,而Hibernate期望String作为Exception状态。