我正在将Kodo JDO项目迁移到EclipseLink,并在尝试将SINGLE_TABLE继承实体映射到另一个实体时陷入困境。我收到“将nvarchar值'Audit'转换为数据类型int时转换失败”,却不知道如何解决。
请帮助。
这是我的数据库的外观:
审计表:保存有关用户的审计信息。
用户表-> user_id为PK。
用户ID 100具有2个电话号码审核行。我无法收集结果。 Database - Sample data
基本类别:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Entity
@DiscriminatorColumn(name = "type")
@Table(name = "audit")
@NamedQuery(name = "Audit.findAll", query = "SELECT m FROM Audit m")
public class Audit implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "audit_id")
private int auditId;
@Column(name = "user_id")
private int userId;
@OneToMany(mappedBy = "addressAudit1")
private List<User> User;
//Getters and Setters are defined.
}
子类1:
@Entity
@DiscriminatorValue("1")
public class Address extends Audit
{
@Column(name = "street_addr1")
private String streetAddr1;
@Column(name = "street_addr2")
private String streetAddr2;
//Getters and Setters are defined.
}
子类2:
@Entity
@DiscriminatorValue("2")
public class Phone extends Audit
{
@Column(name = "home_phone")
private String homePhone;
@Column(name = "home_phone_ext")
private String homePhoneExt;
@Column(name = "work_phone")
private String workPhone;
@Column(name = "work_phone_ext")
private String workPhoneExt;
//Getters and Setters are defined.
}
用户类别
@Entity
@Table(name = "user")
@NamedQuery(name = "User.findAll", query = "SELECT m FROM User m")
public class User implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "user_id")
private int UserId;
*//Below code is causing issue*
@ManyToOne
@JoinColumn(name = "CURRENT_STREET")
private Audit addressAudit1;
//Getters and Setters are defined.
}
UseCase 1.我需要根据ID搜索用户。这是我的搜索方式
public static User getUserForId(long userID, EntityManagerFactory emf) throws Exception
{
User user = null;
EntityManager em = null;
try
{
em = emf.createEntityManager();
TypedQuery<User> q = em.createQuery("select u from " + User.class.getName() + " u where u.userId = :userIndex", User.class);
q.setParameter("userIndex", userID);
List<User> userList = q.getResultList();
if (userList.size() > 0)
{
Iterator<User> userItr = userList.iterator();
if (userItr.hasNext())
{
user = userItr.next();
}
}
}
catch (Exception e)
{
logger.error(e.getMessage());
throw new Exception(e.getMessage());
}
finally
{
em.close();
}
return user;
}
在q.getResultList()处引发异常。
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the nvarchar value 'Audit' to data type int.
Error Code: 245
Call: SELECT audit_id, type, JDOVERSION, user_id FROM audit WHERE ((audit_id = ?) AND (type = ?))
bind => [2 parameters bound]
Query: ReadObjectQuery(name="addressAudit1" referenceClass=Audit )
注意:使用@embedded和@embeddable批注时,其工作原理相同。我放弃了它,因为我无法获取审计集合。