В некоторых тестах у меня появляется ошибка:
SqlExceptionHelper - Invalid column name 'contact_information_id'.
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
В БД вместо этого у меня есть столбец primary_contact_information_id .
Происходит, когда хранилище пытается сохранить объект Organization .В классе Organization есть поле PrimaryContact , а в классе PrimaryContact У меня есть поле contactInformation .
Проект был перемещен из java7 вjava8 и в настоящее время я работаю над миграцией с Hibernate 3 на Hibernate 5.
Была стратегия именования org.hibernate.cfg.ImprovedNamingStrategy, поэтому для hibernate 5 я использовал CustomNamingStrategy
public class CustomNamingStrategy extends PhysicalNamingStrategyStandardImpl {
public static final CustomNamingStrategy INSTANCE = new CustomNamingStrategy();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
protected static String addUnderscores(String name) {
final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < buf.length() - 1; i++) {
if (
Character.isLowerCase(buf.charAt(i - 1)) &&
Character.isUpperCase(buf.charAt(i)) &&
Character.isLowerCase(buf.charAt(i + 1))) {
buf.insert(i++, '_');
}
}
return buf.toString().toLowerCase(Locale.ROOT);
}
}
Организация
@Entity(name = "Organisation")
@Table(name = "organisation", uniqueConstraints = {@UniqueConstraint(name = "organisation_name_udx", columnNames = {"name"})})
@EntityListeners(EntityCreateListener.class)
public class Organisation implements Serializable, PatientListEntity {
// Some fields here
@Embedded
@Valid
private PrimaryContact primaryContact;
// Some fields here
}
PrimaryContact
@Embeddable
public class PrimaryContact extends Person implements Serializable {
private static final long serialVersionUID = 1L;
@Valid
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "primary_contact_information_id")
private ContactInformation contactInformation;
}
Контактная информация
@Entity(name = "ContactInformation")
@Table(name = "contact_information")
@Data
@NoArgsConstructor
public class ContactInformation implements Serializable {
public static final int MAX_PHONENUMBERS = 5;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Valid
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id")
private Address address = new Address();
@RFCEmail
@Size(max = RFCEmailValidator.EMAIL_TOTAL_MAX_LENGTH)
private String email = "";
@Valid
@Embedded
@AttributeOverrides({@AttributeOverride(name = "number", column = @Column(name = "phone_number_0")),
@AttributeOverride(name = "type", column = @Column(name = "phone_type_0"))})
private PhoneNumber phoneNumber0;
@Valid
@Embedded
@AttributeOverrides({@AttributeOverride(name = "number", column = @Column(name = "phone_number_1")),
@AttributeOverride(name = "type", column = @Column(name = "phone_type_1"))})
private PhoneNumber phoneNumber1;
@Valid
@Embedded
@AttributeOverrides({@AttributeOverride(name = "number", column = @Column(name = "phone_number_2")),
@AttributeOverride(name = "type", column = @Column(name = "phone_type_2"))})
private PhoneNumber phoneNumber2;
@Valid
@Embedded
@AttributeOverrides({@AttributeOverride(name = "number", column = @Column(name = "phone_number_3")),
@AttributeOverride(name = "type", column = @Column(name = "phone_type_3"))})
private PhoneNumber phoneNumber3;
@Valid
@Embedded
@AttributeOverrides({@AttributeOverride(name = "number", column = @Column(name = "phone_number_4")),
@AttributeOverride(name = "type", column = @Column(name = "phone_type_4"))})
private PhoneNumber phoneNumber4;
@OneToOne
@JoinColumn(name = "time_zone_code")
private TimeZone timeZone;
}
Как видите, под полем contactInformation есть @JoinColumn (name = "primary_contact_information_id"), но это не помогает.
!!!ОБНОВЛЕНО !!!
Похоже, это странное поведение происходит из-за моей собственной стратегии именования.Итак, я решил удалить его на всех.Но теперь, после добавления необходимых @Column
аннотаций, я получаю новую ошибку:
BatchUpdateException: The UPDATE statement conflicted with the FOREIGN KEY constraint "application_dme_provider_fk1". The conflict occurred in database "ruslans_00_develop_dev_easycare", table "dbo.application", column 'application_code'.], SQL: update application_dme_provider set application_code=? where application_code=? and party_id=? and party_role_type_code=?