Hibernate 遇到一个未分类的注释 class
Hibernate encountered a non-categorized annotated class
我遇到了这个错误:
org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl - Encountered a non-categorized annotated class [com.propfinancing.marketing.model.Property]; ignoring
我不明白这是什么意思,在 Internet 上搜索也没有太大帮助。
这是我的模型class:
package com.propfinancing.marketing.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name="Property")
@Table(name="property")
public class Property {
@Id
@GeneratedValue
@Column(name="id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
我正在尝试使用 MariaDB 后端数据库编写 Spring-Hibernate webapp。
我要做的第一步是让 Hibernate 为我创建模式。我编写了一个命令行程序来使用 SchemaExport 自动创建表。这是什么代码:
package com.propfinancing.marketing.bin;
import com.propfinancing.marketing.model.Property;
import java.io.File;
import java.io.FileReader;
import java.util.EnumSet;
import java.util.Properties;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
public class CreateTables {
public static void main(String[] args)
throws Exception {
FileReader reader = new FileReader(Paths.CLASSES.getAbsolutePath()+File.separator+"application.properties");
Properties properties = new Properties();
properties.load(reader);
properties.setProperty("hibernate.hbm2ddl.auto", "create");
Configuration cfg = new Configuration();
cfg.setProperties(properties);
StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
sb.applySettings(cfg.getProperties());
StandardServiceRegistry standardServiceRegistry = sb.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
metadataSources.addAnnotatedClass(Property.class);
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadataSources.buildMetadata());
}
}
这很奇怪,我检查了 org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl 的来源,它似乎是从这个方法产生的错误:
private void categorizeAnnotatedClass(Class<?> annotatedClass, AttributeConverterManager attributeConverterManager) {
final XClass xClass = reflectionManager.toXClass( annotatedClass );
// categorize it, based on assumption it does not fall into multiple categories
if ( xClass.isAnnotationPresent( Converter.class ) ) {
//noinspection unchecked, rawtypes
attributeConverterManager.addAttributeConverter( (Class<? extends AttributeConverter>) annotatedClass );
}
else if ( xClass.isAnnotationPresent( Entity.class )
|| xClass.isAnnotationPresent( MappedSuperclass.class ) ) {
xClasses.add( xClass );
}
else if ( xClass.isAnnotationPresent( Embeddable.class ) ) {
xClasses.add( xClass );
}
else {
log.debugf( "Encountered a non-categorized annotated class [%s]; ignoring", annotatedClass.getName() );
}
}
但是,我的坚持 class 是用@Entity 注释的,所以应该没问题。
知道为什么 Hibernate 不喜欢我的模型 class 吗?
我想通了。我正在使用自动迁移到 Jakarta EE 规范的 war 文件将所有内容加载到 Tomcat。将测试文件迁移到 Jakarta EE 后,一切正常。
我遇到了这个错误:
org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl - Encountered a non-categorized annotated class [com.propfinancing.marketing.model.Property]; ignoring
我不明白这是什么意思,在 Internet 上搜索也没有太大帮助。
这是我的模型class:
package com.propfinancing.marketing.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name="Property")
@Table(name="property")
public class Property {
@Id
@GeneratedValue
@Column(name="id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
我正在尝试使用 MariaDB 后端数据库编写 Spring-Hibernate webapp。
我要做的第一步是让 Hibernate 为我创建模式。我编写了一个命令行程序来使用 SchemaExport 自动创建表。这是什么代码:
package com.propfinancing.marketing.bin;
import com.propfinancing.marketing.model.Property;
import java.io.File;
import java.io.FileReader;
import java.util.EnumSet;
import java.util.Properties;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
public class CreateTables {
public static void main(String[] args)
throws Exception {
FileReader reader = new FileReader(Paths.CLASSES.getAbsolutePath()+File.separator+"application.properties");
Properties properties = new Properties();
properties.load(reader);
properties.setProperty("hibernate.hbm2ddl.auto", "create");
Configuration cfg = new Configuration();
cfg.setProperties(properties);
StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
sb.applySettings(cfg.getProperties());
StandardServiceRegistry standardServiceRegistry = sb.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
metadataSources.addAnnotatedClass(Property.class);
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadataSources.buildMetadata());
}
}
这很奇怪,我检查了 org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl 的来源,它似乎是从这个方法产生的错误:
private void categorizeAnnotatedClass(Class<?> annotatedClass, AttributeConverterManager attributeConverterManager) {
final XClass xClass = reflectionManager.toXClass( annotatedClass );
// categorize it, based on assumption it does not fall into multiple categories
if ( xClass.isAnnotationPresent( Converter.class ) ) {
//noinspection unchecked, rawtypes
attributeConverterManager.addAttributeConverter( (Class<? extends AttributeConverter>) annotatedClass );
}
else if ( xClass.isAnnotationPresent( Entity.class )
|| xClass.isAnnotationPresent( MappedSuperclass.class ) ) {
xClasses.add( xClass );
}
else if ( xClass.isAnnotationPresent( Embeddable.class ) ) {
xClasses.add( xClass );
}
else {
log.debugf( "Encountered a non-categorized annotated class [%s]; ignoring", annotatedClass.getName() );
}
}
但是,我的坚持 class 是用@Entity 注释的,所以应该没问题。
知道为什么 Hibernate 不喜欢我的模型 class 吗?
我想通了。我正在使用自动迁移到 Jakarta EE 规范的 war 文件将所有内容加载到 Tomcat。将测试文件迁移到 Jakarta EE 后,一切正常。