缺少列列错错误的列名

时间:2015-08-17 18:28:25

标签: java spring hibernate postgresql

下面的详细信息,但简短版本是:我在名为user_id的PostgreSQL表上有一个列config。该列是使用@Column(name = "user_id")引用的,但是当我的服务器启动时,无法构建EntityManagerFactory,因为:

Caused by: org.hibernate.HibernateException: Missing column: userId in public.config
    at org.hibernate.mapping.Table.validateColumns(Table.java:366)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1305)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:512)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    ... 31 more

所以我似乎错过了一些导致Hibernate查找userId列而不是名为user_id的列的内容。

详细说明:

表格定义:

CREATE TABLE config
(
  user_id character varying(255) NOT NULL,
  application_id integer NOT NULL,
  step_id integer NOT NULL,
  prompt_id integer NOT NULL,
  default_value character varying(255),
  CONSTRAINT "PK_Config" PRIMARY KEY (user_id, application_id, step_id, prompt_id),
  CONSTRAINT "FK_Config_User" FOREIGN KEY (user_id)
      REFERENCES app_user (user_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE config
  OWNER TO appuser;

Hibernate实体定义为:

// ...

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

@Entity
@Table(name = "config")
@IdClass(ConfigId.class)
public class Config {

    private String userId;
    private Integer applicationId;
    private Integer stepId;
    private Integer promptId;
    private String defaultValue;

    @Id
    @Column(name = "user_id")
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Id
    @Column(name = "application_id")
    public Integer getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(Integer applicationId) {
        this.applicationId = applicationId;
    }

    @Id
    @Column(name = "step_id")
    public Integer getStepId() {
        return stepId;
    }

    public void setStepId(Integer stepId) {
        this.stepId = stepId;
    }

    @Id
    @Column(name = "prompt_id")
    public Integer getPromptId() {
        return promptId;
    }

    public void setPromptId(Integer promptId) {
        this.promptId = promptId;
    }

    @Column(name = "default_value", columnDefinition = "nvarchar")
    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

}

其中ConfigId定义为:

// ...

public class ConfigId implements Serializable{

    private static final long serialVersionUID = -5372509566187824168L;

    private String userId;
    private Integer applicationId;
    private Integer stepId;
    private Integer promptId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(Integer applicationId) {
        this.applicationId = applicationId;
    }

    public Integer getStepId() {
        return stepId;
    }

    public void setStepId(Integer stepId) {
        this.stepId = stepId;
    }

    public Integer getPromptId() {
        return promptId;
    }

    public void setPromptId(Integer promptId) {
        this.promptId = promptId;
    }

    @Override
    public int hashCode() {
       // ...
    }

    @Override
    public boolean equals(Object obj) {
       // ...
    }
}

3 个答案:

答案 0 :(得分:0)

您的代码中缺少复合键。这个例子已经得到了很好的解释:Composite-Key and Hibernate

答案 1 :(得分:0)

垃圾。我有另一个实体用@JoinColumn(name = "userId")引用了我的Config实体。我能够解决这个问题,现在一切正常。我真的希望错误信息更清楚“userId”的来源。

答案 2 :(得分:0)

我遇到了完全相同的问题。我的问题似乎是定义了@column注释的混合用法。

在我的实体类中,@ column注释在getter方法中定义。为我的复合id创建@Embeddable类时,@ field注释是在字段级别定义的。我确保所有@Column注释都是我的getter方法,并且一切都开始工作了。