Hibernate在列重命名后生成旧查询

时间:2015-08-03 07:52:36

标签: java hibernate jpa

我已经在我的数据库中重命名了列(从configuration_SYSTEM_ID到SYSTEM_ID),并将AttributeOverride注释添加到了我的AbstractSubConfiguration类。但是hibernate会忽略此批注并继续使用旧列名生成查询(configuration_SYSTEM_ID而不是SYSTEM_ID)。

我的课程如下:

AbstractSubConfiguration.java

       @MappedSuperclass
        @IdClass(Configuration.class)
        public class AbstractSubConfiguration implements Dto, Serializable {
            private static final long serialVersionUID = -6271877313478924753L;

            @Id
            @OneToOne(fetch = FetchType.LAZY)
    @AttributeOverride(name = "systemId", column = @Column(name = "SYSTEM_ID"))
            private Configuration configuration;

            public AbstractSubConfiguration() {
                super();
            }

            public AbstractSubConfiguration(final Configuration configuration) {
                super();
                this.configuration = configuration;
            }

            @XmlTransient
            public final Configuration getConfiguration() {
                return configuration;
            }

            public final void setConfiguration(final Configuration configuration) {
                this.configuration = configuration;
            }

        }

Configuration.java

    Entity(name = "CONFIGURATION")
    public class Configuration implements Dto, Serializable {
        private static final long serialVersionUID = 6601197795258837065L;

        @EmbeddedId
        private ConfigurationId configurationId;

        @Column(name = "CONFIGURATION_NAME")
        private String configurationName;

        @OneToOne(cascade = CascadeType.PERSIST, mappedBy = "configuration", fetch = FetchType.EAGER)
        private BasicConfiguration basicConfiguration;
        @OneToOne(cascade = CascadeType.PERSIST, mappedBy = "configuration", fetch = FetchType.EAGER)
        private AdvancedConfiguration advancedConfiguration;

.....


...
}

ConfigurationId.java

@Embeddable
public class ConfigurationId implements Serializable {
    private static final long serialVersionUID = -5123943430808049180L;

    private SystemId systemId;
    @Column(name = "CONFIGURATION_ID")
    private int configurationId;

    public ConfigurationId(final SystemId systemId, final int configurationId) {
        super();
        this.systemId = systemId;
        this.configurationId = configurationId;
    }
    ....

    ....
}

结果我有一个错误2695 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 未知列' tes0_.configuration_SYSTEM_ID'在'字段列表'

2 个答案:

答案 0 :(得分:0)

来自doc @AttributeOverride

  

java.lang.String名称             (必需)如果正在使用基于属性的访问,则覆盖其映射的属性的名称,或者名称   字段,如果使用基于字段的访问。

在您的地图中,我看不到systemId属性,而是看到configurationId属性

修改

根据@AttributeOverride中的修改名称,应为configuration.systemId

请参阅java doc的带有zipcode的示例:

@Embeddable public class Address {
        protected String street;
        protected String city;
        protected String state;
        @Embedded protected Zipcode zipcode;
    }

    @Embeddable public class Zipcode {
        protected String zip;
        protected String plusFour;
    }

    @Entity public class Customer {
        @Id protected Integer id;
        protected String name;
        @AttributeOverrides({
            @AttributeOverride(name="state",
                               column=@Column(name="ADDR_STATE")),
            @AttributeOverride(name="zipcode.zip",
                               column=@Column(name="ADDR_ZIP"))
        })
        @Embedded protected Address address;
        ...
    }

答案 1 :(得分:0)

以这种方式解决了这个问题:

       @MappedSuperclass
        @IdClass(Configuration.class)
        public class AbstractSubConfiguration implements Dto, Serializable {
            private static final long serialVersionUID = -6271877313478924753L;

            @Id
            @OneToOne(fetch = FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name = "SYSTEM_ID", referencedColumnName = "SYSTEM_ID"),
        @JoinColumn(name = "configuration_CONFIGURATION_ID", referencedColumnName = "CONFIGURATION_ID")})            
private Configuration configuration;


...


....
}