1

我一直在为这个而烦恼。我有一个像这样拉对象的类:

public UserDTO getUser(String login)
{
    String jql = "select entity from User as entity  where entity.abcUserId = :userID ";

    Query query = _entityManager.createQuery(jql)
            .setParameter("userID", login);
    try
    {
        Object user = query.getSingleResult();
        return ((User) user).extractObject();
    }
    catch(NoResultException e)
    {
        LOG.error(e.getMessage());
        return null;
    }

}

随着班级的拉动:

@Entity
@Table(name="ABC_User")
public class User implements Serializable,EntityObject<UserDTO>
{

private static final long   serialVersionUID    = 1L;

private Long id;
private String abcUserId;
private Company company;
private List<UserPreference> userPreferences;

@Id
@GeneratedValue
public Long getId()
{
    return id;
}

public void setId(Long id)
{
    this.id = id;
}

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Company_ID", nullable=false)
public Company getCompany()
{
    return company;
}

public void setCompany(Company customer)
{
    this.company = customer;
}

/**
 * @return the userPreference
 */
@ElementCollection(fetch=FetchType.EAGER)
@JoinTable(name="ABC_USER_PREFERENCES")
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
public List<UserPreference> getUserPreferences()
{
    if(userPreferences == null)
    {
        userPreferences = new ArrayList<UserPreference>();
    }
    return userPreferences;
}

/**
 * @param userPreference the userPreference to set
 */
public void setUserPreferences(List<UserPreference> userPreferences)
{
    this.userPreferences = userPreferences;
}

/**
 * @return the abcUserId
 */
@Column(name="ABC_USER_ID", length=10, nullable=false)
public String getAbcUserId()
{
    return abcUserId;
}

/**
 * @param abcUserId the abcUserId to set
 */
public void setAbcUserId(String abcUserId)
{
    this.abcUserId = abcUserId;
}

其中引用了这个可嵌入对象:

@Embeddable
public class UserPreference implements Serializable
{
private static final long   serialVersionUID    = 1L;

private String prefKey;
private String prefValue;

public UserPreference() {}

public UserPreference(String key, String value)
{
    this.prefKey = key;
    this.prefValue = value;
}

@Column(nullable=false, length=255)
public String getPrefKey()
{
    return prefKey;
}
public void setPrefKey(String key)
{
    this.prefKey = key;
}
@Column(nullable=false, length=1048576)
public String getPrefValue()
{
    return prefValue;
}
public void setPrefValue(String value)
{
    this.prefValue = value;
}
}

所以,冗长的代码块被审查了一些东西,这是行不通的。每次我尝试从数据库中提取用户时,它都会抛出“SQLServerException: Invalid column name 'User_id'”。User_id 在我的项目中从未被引用(我已经检查过),它始终是abc_user。我可以看到对象在 Eclipse 调试器中被拉到一起,它达到了添加 UserPreferences 列表的地步,然后分崩离析。如果我注释掉 User 类的 UserPreferences 部分,它会成功提取(并在使用它们的其他地方中断)。

我错过了什么?

4

2 回答 2

0

请参阅以下代码部分

@Column(name="ABC_USER_ID", length=10, nullable=false)
public String getAbcUserId()
{
    return abcUserId;
}

检查表是否有列名“ABC_USER_ID”。然后,尝试将此代码放在 @JoinTable 部分之前

于 2015-06-03T17:17:08.713 回答
0

尝试定义@Column. User.id它有一个@Id,但没有@Column。可能是 Hibernate 假定该字段的列是 _id,从而导致“User_id”,然后当它尝试将返回的数据映射到对象时找不到它。

于 2015-09-16T13:54:21.543 回答