Hibernate5's new way of creating SessionFactory, using Hibernate4's method to report exception XXX is not mapped

How Hibernate4 creates SessionFactory
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
		 .applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);


Hibernate5 creates a new way of SessionFactory
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
				.configure().build();
Metadata metadata = new MetadataSources(standardRegistry)
		.getMetadataBuilder()
		.applyImplicitNamingStrategy(
				ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
		.build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();


Hibernate5 no longer explicitly uses the Configuration class to represent configuration information, but adds a Metadata class to handle it. The creation of the SessionFactory obtains a creator from the Metadata instance, and then calls the build() method of the creator to obtain the SessionFactory. This is the typical creator pattern.
If you still use Hibernate4 to create SessionFactory in Hibernate5, the code compilation will not report an error, but a QuerySyntaxException will be thrown at runtime : XXX is not mapped .

This exception message seems to tell us that the User class is not mapped to the database table, however, I carefully checked all the configuration information and found no mismatch. So I downgraded Hibernate5, which Maven depends on, to the last version of Hibernate4, 4.3.11. The QuerySyntaxException exception did not appear, and the User class was mapped to the database table normally. I thought it was a bug in Hibernate5 at first, but after checking the documentation, I found out that the way the SessionFactory was created was changed.

The detailed exception information is as follows:
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User as u where u.name = :name and u.password = :password]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
	at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
	at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240)
	at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1907)
	at org.jisonami.service.DataRepository.queryAll (DataRepository.java:84)
	at org.jisonami.service.UserService.validate(UserService.java:28)
	at org.jisonami.controller.account.AccountController.login(AccountController.java:40)
	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:483)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:178)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)


hibernate.cfg.xml configuration information, this is unchanged from hibernate4
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.75.130:1521:orcl</property>
        <property name="hibernate.connection.username">jison</property>
        <property name="hibernate.connection.password">jison</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        
        <!-- Automatically create the required database tables-->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- Print the sql statement in the console-->
        <property name="hibernate.show_sql">true</property>
        
        <mapping class="org.jisonami.entity.User" />
    </session-factory>
</hibernate-configuration>


The configuration of the User class uses the annotation method, which is also unchanged from Hibernate4.
package org.jisonami.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="t_user")
public class User implements Serializable{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public User(){}
	@Id
	@GeneratedValue(generator="usergenerator")
	@GenericGenerator(name="usergenerator", strategy="uuid")
	private String id;
	private String name;
	private String password;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}


For more detailed code, see my Github project: https://github.com/jisonami/Jisonami2
At present, my personal project is still relatively rough, and it will be better in the future.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326983271&siteId=291194637
Recommended