我在一个独立的maven java应用程序中使用hibernate 5.0.6和hibernate注释3.5.6-Final和mysql 5.1.37。
我正在尝试使用简单的持久性示例,但在调用save时收到以下错误:
0 [main] ERROR edu.uci.ics.crawler4j.crawler.CrawlController - Error happened
org.hibernate.MappingException: Unknown entity: br.com.alexpfx.supermarket.crawler.model.domain.Product
通过配置文件中的映射类指向类。但它无法找到。
<mapping class="br.com.alexpfx.supermarket.crawler.model.domain.Product" />
但是当我在HibernateUtil中执行此操作时:
configure.addAnnotatedClass(Product.class);
configure.addAnnotatedClass(Manufacturer.class);
有效。但我想指出在xml文件中映射类。
我发现了几个与此相关的错误但发现没有解决方案解决了我的问题。我想我做错了。
错误的完整堆栈跟踪:
0 [main] ERROR edu.uci.ics.crawler4j.crawler.CrawlController - Error happened
org.hibernate.MappingException: Unknown entity: br.com.alexpfx.supermarket.crawler.model.domain.Product
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at com.sun.proxy.$Proxy19.save(Unknown Source)
at br.com.alexpfx.supermarket.crawler.crawler.mercadoribeirao.MercadoRibeiraoCrawler.init(MercadoRibeiraoCrawler.java:47)
at br.com.alexpfx.supermarket.crawler.crawler.Crawler.<init>(Crawler.java:16)
at br.com.alexpfx.supermarket.crawler.crawler.mercadoribeirao.MercadoRibeiraoCrawler.<init>(MercadoRibeiraoCrawler.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at edu.uci.ics.crawler4j.crawler.CrawlController.start(CrawlController.java:158)
at edu.uci.ics.crawler4j.crawler.CrawlController.start(CrawlController.java:133)
at br.com.alexpfx.supermarket.crawler.crawler.CrawlerStarter.start(CrawlerStarter.java:41)
at br.com.alexpfx.supermarket.crawler.Main.save(Main.java:29)
at br.com.alexpfx.supermarket.crawler.Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
配置文件位于:project \ src \ main \ resources \ hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/smket</property>
<property name="connection.username">alex</property>
<property name="connection.password">123alex</property>
<property name="connection.pool_size">5</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="br.com.alexpfx.supermarket.crawler.model.domain.Product" />
<mapping class="br.com.alexpfx.supermarket.crawler.model.domain.Manufacturer" />
</session-factory>
</hibernate-configuration>
Hibernate util:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
/*
configure.addAnnotatedClass(Product.class);
configure.addAnnotatedClass(Manufacturer.class);
*/
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
return configure.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}
}
pojo类:
package br.com.alexpfx.supermarket.crawler.model.domain;
import javax.persistence.*;
@Entity
@Table(name = "tb_produtos")
public class Product implements BaseEntity {
public Product() {
}
Product(Integer id, Manufacturer manufacturer, String description, String url, Keywords keywords) {
this.id = id;
this.manufacturer = manufacturer;
this.description = description;
this.url = url;
this.keywords = keywords;
}
@Id
@GeneratedValue
private Integer id;
@ManyToOne
@JoinColumn(name = "ID_FABRICANTE")
private Manufacturer manufacturer;
@Column(name = "DESCRICAO")
private String description;
@Column(name = "URL")
private String url;
@Transient
private Keywords keywords;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Manufacturer getManufacturer() {
return manufacturer;
}
public void setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Keywords getKeywords() {
return keywords;
}
public void setKeywords(Keywords keywords) {
this.keywords = keywords;
}
}
答案 0 :(得分:2)
Hibernate 5配置构建已经是一个熟悉的问题了。你不能使用Hibernate 4配置方法来配置Hibernate 5.所以只需使用这个
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
您可以参考this了解其他说明。我已经实现了一个适用于Hibernate 4和Hibernate 5的配置构建器,您也可以查看它ConfigurationBuilder。
答案 1 :(得分:1)
您没有指定hibernate配置文件hibernate.cfg.xml
的位置。
hibernate.cfg.xml
文件是设置与数据库交互所需的所有配置的位置。因此,数据库在那里定义,以及数据库用户凭据。方言设置为MySQL,驱动程序为com.mysql.jdbc.Driver
。还有一个mapping属性,其中定义了实体类。
您还可以在那里设置特定的数据库选项,例如每次创建sessionFactory
时是创建架构还是仅更新架构。这是在hibernate.hbm2ddl.auto
属性中配置的,该属性设置为update
。因此架构只会更新。如果将此属性设置为create,则每次运行应用程序时,都会重新创建架构,从而删除以前的数据。此处设置的另一个属性是show_sql
,它指定sql查询是否将显示在控制台或记录器中。
首先,getSessionFactory()
是一种方法,它提供SessionFactory
,Session
的创建者,Java应用程序和Hibernate之间的基本接口。 SessionFactory
使用StandardServiceRegistryBuilder
构建,使用Configuration
。您可以在Configuration
指定创建SessionFactory
时要使用的属性和映射文档。
因此,与数据库交互的每个方法都会获得Session
,使用getSessionFactory()
,并使用openSession()
的{{1}} API方法。
SessionFactory