JPA 实体——org.hibernate.TypeMismatchException
环境: JDK 1.6、JEE5 Hibernate 核心 3.3.1.GA、Hibernate 注释 3.4.0.GA DB:Informix
使用逆向工程从数据库模式创建我的持久性实体 [注意:这是我无法更改的工作模式]
选择 basic_auth_accounts org.hibernate.TypeMismatchException 列表时出现异常:提供的 id ebusiness.weblogic.model.UserAccounts 类的类型错误。预期:类 ebusiness.weblogic.model.UserAccountsId,得到类 ebusiness.weblogic.model.BasicAuthAccountsId
basic_auth_accounts 和 user_accounts 都具有复合主键和一对一关系。 有什么线索可以在这里做什么吗?这对于我让它发挥作用非常重要。在网上找不到任何实质性的解决方案,有些人说创建一个hibernate已经完成的ID类,有些人说不要有一对一的关系。
请帮我!!
/**
* BasicAuthAccounts generated by hbm2java
*/
@Entity
@Table(name = "basic_auth_accounts", schema = "ebusdevt", catalog = "ebusiness_dev", uniqueConstraints = @UniqueConstraint(columnNames = {
"realm_type_id", "realm_qualifier", "account_name" }))
public class BasicAuthAccounts implements java.io.Serializable {
private BasicAuthAccountsId id;
private UserAccounts userAccounts;
private String accountName;
private String hashedPassword;
private boolean passwdChangeReqd;
private String hashMethodId;
private int failedAttemptNo;
private Date failedAttemptDate;
private Date lastAccess;
public BasicAuthAccounts() {
}
public BasicAuthAccounts(UserAccounts userAccounts, String accountName, String hashedPassword,
boolean passwdChangeReqd, String hashMethodId, int failedAttemptNo) {
this.userAccounts = userAccounts;
this.accountName = accountName;
this.hashedPassword = hashedPassword;
this.passwdChangeReqd = passwdChangeReqd;
this.hashMethodId = hashMethodId;
this.failedAttemptNo = failedAttemptNo;
}
public BasicAuthAccounts(UserAccounts userAccounts, String accountName, String hashedPassword,
boolean passwdChangeReqd, String hashMethodId, int failedAttemptNo,
Date failedAttemptDate, Date lastAccess) {
this.userAccounts = userAccounts;
this.accountName = accountName;
this.hashedPassword = hashedPassword;
this.passwdChangeReqd = passwdChangeReqd;
this.hashMethodId = hashMethodId;
this.failedAttemptNo = failedAttemptNo;
this.failedAttemptDate = failedAttemptDate;
this.lastAccess = lastAccess;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "realmTypeId", column = @Column(name = "realm_type_id", nullable = false, length = 32)),
@AttributeOverride(name = "realmQualifier", column = @Column(name = "realm_qualifier", nullable = false, length = 32)),
@AttributeOverride(name = "accountId", column = @Column(name = "account_id", nullable = false)) })
public BasicAuthAccountsId getId() {
return this.id;
}
public void setId(BasicAuthAccountsId id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
@NotNull
public UserAccounts getUserAccounts() {
return this.userAccounts;
}
public void setUserAccounts(UserAccounts userAccounts) {
this.userAccounts = userAccounts;
}
/**
* BasicAuthAccountsId generated by hbm2java
*/
@Embeddable
public class BasicAuthAccountsId implements java.io.Serializable {
private String realmTypeId;
private String realmQualifier;
private long accountId;
public BasicAuthAccountsId() {
}
public BasicAuthAccountsId(String realmTypeId, String realmQualifier, long accountId) {
this.realmTypeId = realmTypeId;
this.realmQualifier = realmQualifier;
this.accountId = accountId;
}
/**
* UserAccounts generated by hbm2java
*/
@Entity
@Table(name = "user_accounts", schema = "ebusdevt", catalog = "ebusiness_dev")
public class UserAccounts implements java.io.Serializable {
private UserAccountsId id;
private Realms realms;
private UserDetails userDetails;
private Integer accessLevel;
private String status;
private boolean isEdge;
private String role;
private boolean chargesAccess;
private Date createdTimestamp;
private Date lastStatusChangeTimestamp;
private BasicAuthAccounts basicAuthAccounts;
private Set<Sessions> sessionses = new HashSet<Sessions>(0);
private Set<AccountGroups> accountGroupses = new HashSet<AccountGroups>(0);
private Set<UserPrivileges> userPrivilegeses = new HashSet<UserPrivileges>(0);
public UserAccounts() {
}
public UserAccounts(UserAccountsId id, Realms realms, UserDetails userDetails, String status,
boolean isEdge, boolean chargesAccess) {
this.id = id;
this.realms = realms;
this.userDetails = userDetails;
this.status = status;
this.isEdge = isEdge;
this.chargesAccess = chargesAccess;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "realmTypeId", column = @Column(name = "realm_type_id", nullable = false, length = 32)),
@AttributeOverride(name = "realmQualifier", column = @Column(name = "realm_qualifier", nullable = false, length = 32)),
@AttributeOverride(name = "accountId", column = @Column(name = "account_id", nullable = false)) })
@NotNull
public UserAccountsId getId() {
return this.id;
}
public void setId(UserAccountsId id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "userAccounts")
public BasicAuthAccounts getBasicAuthAccounts() {
return this.basicAuthAccounts;
}
public void setBasicAuthAccounts(BasicAuthAccounts basicAuthAccounts) {
this.basicAuthAccounts = basicAuthAccounts;
}
/**
* UserAccountsId generated by hbm2java
*/
@Embeddable
public class UserAccountsId implements java.io.Serializable {
private String realmTypeId;
private String realmQualifier;
private long accountId;
public UserAccountsId() {
}
public UserAccountsId(String realmTypeId, String realmQualifier, long accountId) {
this.realmTypeId = realmTypeId;
this.realmQualifier = realmQualifier;
this.accountId = accountId;
}
@Column(name = "realm_type_id", nullable = false, length = 32)
@NotNull
@Length(max = 32)
public String getRealmTypeId() {
return this.realmTypeId;
}
public void setRealmTypeId(String realmTypeId) {
this.realmTypeId = realmTypeId;
}
@Column(name = "realm_qualifier", nullable = false, length = 32)
@NotNull
@Length(max = 32)
public String getRealmQualifier() {
return this.realmQualifier;
}
public void setRealmQualifier(String realmQualifier) {
this.realmQualifier = realmQualifier;
}
@Column(name = "account_id", nullable = false)
public long getAccountId() {
return this.accountId;
}
public void setAccountId(long accountId) {
this.accountId = accountId;
}
类的主要代码是:
Environment:
JDK 1.6, JEE5
Hibernate Core 3.3.1.GA, Hibernate Annotations 3.4.0.GA
DB:Informix
Used reverse engineering to create my persistence entities from db schema [NB:This is a schema in work i cannot change]
Getting exception when selecting list of basic_auth_accounts org.hibernate.TypeMismatchException: Provided id of the wrong type for class ebusiness.weblogic.model.UserAccounts. Expected: class ebusiness.weblogic.model.UserAccountsId, got class ebusiness.weblogic.model.BasicAuthAccountsId
Both basic_auth_accounts and user_accounts have composite primary keys and one-to-one relationships.
Any clues what to do here? This is pretty important that i get this to work. Cannot find any substantial solution on the net, some say to create an ID class which hibernate has done, and some say not to have a one-to-one relationship.
Please help me!!
/**
* BasicAuthAccounts generated by hbm2java
*/
@Entity
@Table(name = "basic_auth_accounts", schema = "ebusdevt", catalog = "ebusiness_dev", uniqueConstraints = @UniqueConstraint(columnNames = {
"realm_type_id", "realm_qualifier", "account_name" }))
public class BasicAuthAccounts implements java.io.Serializable {
private BasicAuthAccountsId id;
private UserAccounts userAccounts;
private String accountName;
private String hashedPassword;
private boolean passwdChangeReqd;
private String hashMethodId;
private int failedAttemptNo;
private Date failedAttemptDate;
private Date lastAccess;
public BasicAuthAccounts() {
}
public BasicAuthAccounts(UserAccounts userAccounts, String accountName, String hashedPassword,
boolean passwdChangeReqd, String hashMethodId, int failedAttemptNo) {
this.userAccounts = userAccounts;
this.accountName = accountName;
this.hashedPassword = hashedPassword;
this.passwdChangeReqd = passwdChangeReqd;
this.hashMethodId = hashMethodId;
this.failedAttemptNo = failedAttemptNo;
}
public BasicAuthAccounts(UserAccounts userAccounts, String accountName, String hashedPassword,
boolean passwdChangeReqd, String hashMethodId, int failedAttemptNo,
Date failedAttemptDate, Date lastAccess) {
this.userAccounts = userAccounts;
this.accountName = accountName;
this.hashedPassword = hashedPassword;
this.passwdChangeReqd = passwdChangeReqd;
this.hashMethodId = hashMethodId;
this.failedAttemptNo = failedAttemptNo;
this.failedAttemptDate = failedAttemptDate;
this.lastAccess = lastAccess;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "realmTypeId", column = @Column(name = "realm_type_id", nullable = false, length = 32)),
@AttributeOverride(name = "realmQualifier", column = @Column(name = "realm_qualifier", nullable = false, length = 32)),
@AttributeOverride(name = "accountId", column = @Column(name = "account_id", nullable = false)) })
public BasicAuthAccountsId getId() {
return this.id;
}
public void setId(BasicAuthAccountsId id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
@NotNull
public UserAccounts getUserAccounts() {
return this.userAccounts;
}
public void setUserAccounts(UserAccounts userAccounts) {
this.userAccounts = userAccounts;
}
/**
* BasicAuthAccountsId generated by hbm2java
*/
@Embeddable
public class BasicAuthAccountsId implements java.io.Serializable {
private String realmTypeId;
private String realmQualifier;
private long accountId;
public BasicAuthAccountsId() {
}
public BasicAuthAccountsId(String realmTypeId, String realmQualifier, long accountId) {
this.realmTypeId = realmTypeId;
this.realmQualifier = realmQualifier;
this.accountId = accountId;
}
/**
* UserAccounts generated by hbm2java
*/
@Entity
@Table(name = "user_accounts", schema = "ebusdevt", catalog = "ebusiness_dev")
public class UserAccounts implements java.io.Serializable {
private UserAccountsId id;
private Realms realms;
private UserDetails userDetails;
private Integer accessLevel;
private String status;
private boolean isEdge;
private String role;
private boolean chargesAccess;
private Date createdTimestamp;
private Date lastStatusChangeTimestamp;
private BasicAuthAccounts basicAuthAccounts;
private Set<Sessions> sessionses = new HashSet<Sessions>(0);
private Set<AccountGroups> accountGroupses = new HashSet<AccountGroups>(0);
private Set<UserPrivileges> userPrivilegeses = new HashSet<UserPrivileges>(0);
public UserAccounts() {
}
public UserAccounts(UserAccountsId id, Realms realms, UserDetails userDetails, String status,
boolean isEdge, boolean chargesAccess) {
this.id = id;
this.realms = realms;
this.userDetails = userDetails;
this.status = status;
this.isEdge = isEdge;
this.chargesAccess = chargesAccess;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "realmTypeId", column = @Column(name = "realm_type_id", nullable = false, length = 32)),
@AttributeOverride(name = "realmQualifier", column = @Column(name = "realm_qualifier", nullable = false, length = 32)),
@AttributeOverride(name = "accountId", column = @Column(name = "account_id", nullable = false)) })
@NotNull
public UserAccountsId getId() {
return this.id;
}
public void setId(UserAccountsId id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "userAccounts")
public BasicAuthAccounts getBasicAuthAccounts() {
return this.basicAuthAccounts;
}
public void setBasicAuthAccounts(BasicAuthAccounts basicAuthAccounts) {
this.basicAuthAccounts = basicAuthAccounts;
}
/**
* UserAccountsId generated by hbm2java
*/
@Embeddable
public class UserAccountsId implements java.io.Serializable {
private String realmTypeId;
private String realmQualifier;
private long accountId;
public UserAccountsId() {
}
public UserAccountsId(String realmTypeId, String realmQualifier, long accountId) {
this.realmTypeId = realmTypeId;
this.realmQualifier = realmQualifier;
this.accountId = accountId;
}
@Column(name = "realm_type_id", nullable = false, length = 32)
@NotNull
@Length(max = 32)
public String getRealmTypeId() {
return this.realmTypeId;
}
public void setRealmTypeId(String realmTypeId) {
this.realmTypeId = realmTypeId;
}
@Column(name = "realm_qualifier", nullable = false, length = 32)
@NotNull
@Length(max = 32)
public String getRealmQualifier() {
return this.realmQualifier;
}
public void setRealmQualifier(String realmQualifier) {
this.realmQualifier = realmQualifier;
}
@Column(name = "account_id", nullable = false)
public long getAccountId() {
return this.accountId;
}
public void setAccountId(long accountId) {
this.accountId = accountId;
}
Main Code for classes are:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了跟进在线查找/研究:
据我所见,我有两个表 basic_auth_accounts 和 user_accounts,它们使用相同的复合键realmTypeId、realmQualifier 和 accountId。
两个实体都有带有这些列和一对一关系的嵌入类。
我使用休眠工具从数据库模式生成这些实体,我无法更改!
我想知道我是否对两个实体使用相同的嵌入类,例如将 BasicAuthAccountsId 重命名为更通用的名称,删除 UserAccountsId 并为两个实体使用相同的 @EmbeddedId 类?
just to follow up on looking/researching online:
From what i can see i have two tables basic_auth_accounts and user_accounts that use the same composite keys realmTypeId, realmQualifier and accountId.
Both entities have embeddable classes with these columns and one-to-one relationships.
I used hibernate tools to generate these entities from the DB schema which i cant change!
Im wondering if i used the same embeddable class for both entities, as in rename BasicAuthAccountsId to a more generic name, remove UserAccountsId and for both entities use the same @EmbeddedId class?