我有一个给定的数据库(参见下面的SQL)并尝试将带有类(complexkey)的地图映射为键,但遇到了一个stracktrace并且不知道如何修复它。
我尝试使用这样的属性地图创建一个产品(请参阅repo中的完整代码)。
我想做什么:
ComplexKey complexKey = ComplexKey.builder().locale("de").name("att1").build();
Map<ComplexKey, String> attributes = new HashMap<>();
attributes.put(complexKey, "val1");
Product pro = Product.builder().attributes(attributes).build();
productRepository.saveAndFlush(pro);
但我不知道如何修复Schema验证:在表[complexkey]中缺少列[hash]。 有什么想法吗?
栈跟踪
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) ~[spring-orm-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
... 21 common frames omitted
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [hash] in table [complexkey]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:85) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:473) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
... 27 common frames omitted
我为此问题创建了repository。你只需要克隆,docker-compose up并开始弹簧启动。
COMPLEXKEY
@Embeddable
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ComplexKey {
private String locale;
private String name;
}
产品
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name = "val")
@MapKeyClass(ComplexKey.class)
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="COMPLEXKEY", joinColumns= @JoinColumn(name = "product_id"))
@AttributeOverrides({
@AttributeOverride(name="name", column = @Column(name= "name")),
@AttributeOverride(name="locale", column = @Column(name= "locale")),
})
private Map<ComplexKey, String> attributes;
}
SQL
CREATE TABLE PRODUCT (
id SERIAL primary key,
name varchar(255) not null
);
CREATE TABLE COMPLEXKEY (
locale varchar(255) not null,
name varchar(255) not null,
val varchar(255) not null,
product_id int not null REFERENCES product (id)
);