gpt4 book ai didi

Java Spring 从 java 类创建数据库表给出 BeanCreationException

转载 作者:行者123 更新时间:2023-12-02 11:52:24 24 4
gpt4 key购买 nike

我尝试使用带有 @Entity 注释的 spring 创建一个表,但出现指向 Map 字段的错误。如果可能的话,我希望创建一个包含所有这些字段的表,如果不可能,则仅包含validationGroup字段。

我想为其创建表格的类:

@Entity
public class StatementDocument {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)

@JsonProperty("text")
private String text;

@JsonProperty("statementMap")
private Map<String, AggregatedStatement> statementMap;

@JsonProperty("fields")
private Map<String, String> fields;

@JsonProperty("validationGroup")
private String validationGroup;

public StatementDocument(@JsonProperty("text")String text,
@JsonProperty("statementMap")Map<String, AggregatedStatement> statementMap) {
this.text = text;
this.statementMap = statementMap;
}

@JsonCreator
public StatementDocument(@JsonProperty("text")String text,
@JsonProperty("statementMap")Map<String, AggregatedStatement> statementMap,
@JsonProperty("fields") Map<String,String> fields,
@JsonProperty("validationGroup") String validationGroup) {
this.text = text;
this.statementMap = statementMap;
this.fields = fields;
this.validationGroup=validationGroup;
}

public String getText() {
return text;
}

public Map<String, AggregatedStatement> getStatementMap() {
return statementMap;
}

public void setText(String text) {
this.text = text;
}

public void setStatementMap(Map<String, AggregatedStatement> statementMap) {
this.statementMap = statementMap;
}

public void addField(String key, String value) {
if (fields == null){
fields = new HashMap<>();
}
fields.put(key, value);
}

public String getField(String key) {
return fields.get(key);
}

public Map<String, String> getFields() {
return fields;
}

public void setFields(Map<String, String> fields) {
this.fields = fields;
}

@Override
public String toString() {
return "StatementDocument{" +
"text='" + text + '\'' +
", statementMap=" + statementMap +
", fields=" + fields +
'}';
}

我获取的错误是:

 org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: statement_document, for columns: [org.hibernate.mapping.Column(fields)]

全堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.ibm.fk.textanalys.App.main(App.java:11) [classes/:na]
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.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: statement_document, for columns: [org.hibernate.mapping.Column(fields)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
... 22 common frames omitted

更新 1:

我接受了 Simon Martinelli 提供的排除某些字段在表中创建的答案。

更新 2:

我决定在 StatementDocument 类中包含具有映射数据类型的字段。我将为 AggregateStatement 提供类,以便我可以获得关系映射方面的帮助。

public class AggregatedStatement {

@JsonProperty("statements")
private final List<Statement> statements;

@JsonProperty("outcome")
private final String outcome;

public AggregatedStatement(@JsonProperty("statements") List<Statement> statements,
@JsonProperty("outcome") String outcome) {
this.statements = statements;
this.outcome = outcome;
}

public List<Statement> getStatements() {
return statements;
}

public String getOutcome() {
return outcome;
}

}

最佳答案

您的 map 属性必须是 @ManyToOne/@OnToOne 或 @OneToMany 关系。

@OneToMany 示例

@OneToMany
@MapKey(name = "title")
@JsonProperty("statementMap")
private Map<String, AggregatedStatement> statementMap;

MapKey 是 AggregateStatement 的字段,应用作映射中的键。

在有关排除属性的评论后进行编辑:

如果你想排除实体的某些属性,请使用@Transient注释:

@Entity
public class StatementDocument {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)

@JsonProperty("text")
private String text;

@Transient
@JsonProperty("statementMap")
private Map<String, AggregatedStatement> statementMap;

@Transient
@JsonProperty("fields")
private Map<String, String> fields;

@JsonProperty("validationGroup")
private String validationGroup;
...

关于Java Spring 从 java 类创建数据库表给出 BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com