Hibernate generates old query after renaming columns
I renamed a column in my database (from configuration_SYSTEM_ID to SYSTEM_ID) and added AttributeOverride annotation to my AbstractSubConfiguration class. But hibernate ignores this annotation and continues to generate queries with old column names (configuration_SYSTEM_ID instead of SYSTEM_ID).
My classes look like this:
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;
}
....
....
}
And as a result I have error 2695 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column "tes0_.configuration_SYSTEM_ID" in "field list"
source to share
Resolved it this way:
@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;
...
....
}
source to share
name java.lang.String (Required) The name of the property to be overridden if using property-based access, or name if using field access.
In your mapping I do not see the property systemId
instead I see the configurationId
property
Edit
The edit name match in @AttributeOverride
must beconfiguration.systemId
See example from java doc with 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;
...
}
source to share