Hibernate @Embeddable类为未引用的列抛出“SQLServerException:Invalid column name”

时间:2015-06-03 17:08:08

标签: java hibernate embeddable

我一直在撕扯我的头发。我有一个类拉动对象:

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:无效的列名'User_id'”。 User_id从未在我的项目中引用(我已经检查过),它的总是 abc_user。我可以在Eclipse调试器中看到对象被拉到一起,它到了添加UserPreferences列表然后崩溃的程度。如果我注释掉User类的UserPreferences部分,它会成功拉出(并在其使用它们的地方中断)。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

请参阅以下代码部分

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

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

之前

答案 1 :(得分:0)

尝试为basic_ostream<CharT, Traits>& basic_ostream<CharT, Traits>::operator<<(double); 定义@Column。它有一个User.id,但没有@Id。可能是Hibernate假定该字段的列为_id,导致&#39; User_id&#39;当它尝试将返回的数据映射到对象时它无法找到。