简体   繁体   中英

Hibernate @Embeddable class throws “SQLServerException: Invalid column name” for column that is not referenced

I've been tearing my hair out about this one. I have a class pulling the object like so:

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;
    }

}

With the class it's pulling:

@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;
}

Which references this embeddable object:

@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;
}
}

So, long winded chunk of code out of the way with some stuff censored, this doesn't work. Every time I try to pull a User from the database, it throws "SQLServerException: Invalid column name 'User_id'". User_id is never referenced in my project (I've checked), it's always abc_user. I can watch the object be pulled together in the Eclipse debugger, it gets to the point of adding the list of UserPreferences and then falls apart. If I comment out the UserPreferences portion of the User class, it pulls successfully (and breaks somewhere else where it uses them).

What am I missing?

see the following portion of code

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

Check if table has column name 'ABC_USER_ID'. Then, try placing this code before @JoinTable portion

Try defining @Column for User.id . It has an @Id , but no @Column . It might be that Hibernate assumes the column for that field to be _id, resulting in 'User_id', which it then can't find when it tries to map the returned data to the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM