Hibernate MockMvc不加载命名查询
在tomcat服务器上运行web应用程序时,已正确加载命名查询: 但是当我使用MockMvc时,hibernate找不到任何命名查询。因此,我在执行测试时出错Hibernate MockMvc不加载命名查询,hibernate,spring-mvc,junit,spring-mvc-test,Hibernate,Spring Mvc,Junit,Spring Mvc Test,在tomcat服务器上运行web应用程序时,已正确加载命名查询: 但是当我使用MockMvc时,hibernate找不到任何命名查询。因此,我在执行测试时出错 MockHttpServletResponse: Status = 401 Error message = Named query not found: S_FIND_USER_BY_USERNAME Tomcat日志: 12:40:57 [RMI TCP Connection(3)-127.0.0.1] or
MockHttpServletResponse:
Status = 401
Error message = Named query not found: S_FIND_USER_BY_USERNAME
Tomcat日志:
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaExport.execute (SchemaExport.java:343) - HHH000227: Running hbm2ddl schema export
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - alter table user_authority drop foreign key FK_5losscgu02yaej7prap7o6g5s
Hibernate: alter table user_authority drop foreign key FK_5losscgu02yaej7prap7o6g5s
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists user
Hibernate: drop table if exists user
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists user_authority
Hibernate: drop table if exists user_authority
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists hibernate_sequence
Hibernate: drop table if exists hibernate_sequence
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - create table user (id bigint not null, credentials_expired boolean not null, enabled boolean not null, expired boolean not null, locked boolean not null, password varchar(64) not null, username varchar(32) not null, primary key (id)) ENGINE=InnoDB
Hibernate: create table user (id bigint not null, credentials_expired boolean not null, enabled boolean not null, expired boolean not null, locked boolean not null, password varchar(64) not null, username varchar(32) not null, primary key (id)) ENGINE=InnoDB
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - create table user_authority (id bigint not null, authority varchar(32), user_id bigint not null, primary key (id)) ENGINE=InnoDB
Hibernate: create table user_authority (id bigint not null, authority varchar(32), user_id bigint not null, primary key (id)) ENGINE=InnoDB
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - alter table user add constraint UK_sb8bbouer5wak8vyiiy4pf2bx unique (username)
Hibernate: alter table user add constraint UK_sb8bbouer5wak8vyiiy4pf2bx unique (username)
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - alter table user_authority add index FK_5losscgu02yaej7prap7o6g5s (user_id), add constraint FK_5losscgu02yaej7prap7o6g5s foreign key (user_id) references user (id)
Hibernate: alter table user_authority add index FK_5losscgu02yaej7prap7o6g5s (user_id), add constraint FK_5losscgu02yaej7prap7o6g5s foreign key (user_id) references user (id)
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - create table hibernate_sequence ( next_val bigint )
Hibernate: create table hibernate_sequence ( next_val bigint )
12:40:57 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - insert into hibernate_sequence values ( 1 )
Hibernate: insert into hibernate_sequence values ( 1 )
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.hbm2ddl.SchemaExport.execute (SchemaExport.java:405) - HHH000230: Schema export complete
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.internal.SessionFactoryImpl.checkNamedQueries (SessionFactoryImpl.java:1070) - Checking 1 named HQL queries
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.internal.SessionFactoryImpl.checkNamedQueries (SessionFactoryImpl.java:1080) - Checking named query: S_FIND_USER_BY_USERNAME
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan (QueryPlanCache.java:167) - Unable to locate HQL query plan in cache; generating (SELECT user FROM User user WHERE user.username = :username)
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse (QueryTranslatorImpl.java:267) - parse() - HQL: SELECT user FROM org.mycompany.tracker.security.domain.User user WHERE user.username = :username
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.QueryTranslatorImpl.showHqlAst (QueryTranslatorImpl.java:285) - --- HQL AST ---
\-[QUERY] Node: 'query'
+-[SELECT_FROM] Node: 'SELECT_FROM'
| +-[FROM] Node: 'FROM'
| | \-[RANGE] Node: 'RANGE'
| | +-[DOT] Node: '.'
| | | +-[DOT] Node: '.'
| | | | +-[DOT] Node: '.'
| | | | | +-[DOT] Node: '.'
| | | | | | +-[DOT] Node: '.'
| | | | | | | +-[IDENT] Node: 'org'
| | | | | | | \-[IDENT] Node: 'mycompany'
| | | | | | \-[IDENT] Node: 'tracker'
| | | | | \-[IDENT] Node: 'security'
| | | | \-[IDENT] Node: 'domain'
| | | \-[IDENT] Node: 'User'
| | \-[ALIAS] Node: 'user'
| \-[SELECT] Node: 'SELECT'
| \-[IDENT] Node: 'user'
\-[WHERE] Node: 'WHERE'
\-[EQ] Node: '='
+-[DOT] Node: '.'
| +-[IDENT] Node: 'user'
| \-[IDENT] Node: 'username'
\-[COLON] Node: ':'
\-[IDENT] Node: 'username'
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException (ErrorCounter.java:82) - throwQueryException() : no errors
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.beforeStatement (HqlSqlBaseWalker.java:121) - select << begin [level=1, statement=select]
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.FromElement.doInitialize (FromElement.java:157) - FromClause{level=1} : org.mycompany.tracker.security.domain.User (user) -> user0_
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.FromReferenceNode.setResolved (FromReferenceNode.java:75) - Resolved : user -> user0_.id
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.FromReferenceNode.setResolved (FromReferenceNode.java:75) - Resolved : user -> user0_.id
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.FromElement.handlePropertyBeingDereferenced (FromElement.java:629) - Handling property dereference [org.mycompany.tracker.security.domain.User (user) -> username (class)]
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.DotNode.getDataType (DotNode.java:617) - getDataType() : username -> org.hibernate.type.StringType@7e027403
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.tree.FromReferenceNode.setResolved (FromReferenceNode.java:75) - Resolved : user.username -> user0_.username
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.beforeStatementCompletion (HqlSqlBaseWalker.java:125) - select : finishing up [level=1, statement=select]
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery (HqlSqlWalker.java:630) - processQuery() : ( SELECT ( {select clause} user0_.id ) ( FromClause{level=1} user user0_ ) ( WHERE ( = ( user0_.username user0_.id username ) ? ) ) )
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.util.JoinProcessor.addJoinNodes (JoinProcessor.java:177) - Using FROM fragment [user user0_]
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.afterStatementCompletion (HqlSqlBaseWalker.java:129) - select >> end [level=1, statement=select]
12:40:58 [RMI TCP Connection(3)-127.0.0.1] org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze (QueryTranslatorImpl.java:254) - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (user)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[ALIAS_REF] IdentNode: 'user0_.id as id1_0_' {alias=user, className=org.mycompany.tracker.security.domain.User, tableAlias=user0_}
| \-[SQL_TOKEN] SqlFragment: 'user0_.credentials_expired as credenti2_0_, user0_.enabled as enabled3_0_, user0_.expired as expired4_0_, user0_.locked as locked5_0_, user0_.password as password6_0_, user0_.username as username7_0_'
+-[FROM] FromClause: 'FROM' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[user], fromElementByTableAlias=[user0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'user user0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=user,role=null,tableName=user,tableAlias=user0_,origin=null,columns={,className=org.mycompany.tracker.security.domain.User}}
\-[WHERE] SqlNode: 'WHERE'
\-[EQ] BinaryLogicOperatorNode: '='
+-[DOT] DotNode: 'user0_.username' {propertyName=username,dereferenceType=ALL,propertyPath=username,path=user.username,tableAlias=user0_,className=org.mycompany.tracker.security.domain.User,classAlias=user}
| +-[ALIAS_REF] IdentNode: 'user0_.id' {alias=user, className=org.mycompany.tracker.security.domain.User, tableAlias=user0_}
| \-[IDENT] IdentNode: 'username' {originalText=username}
\-[NAMED_PARAM] ParameterNode: '?' {name=username, expectedType=org.hibernate.type.StringType@7e027403}
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL">
<mapping-file>/db/jpa/domain/User.orm.xml</mapping-file>
<class>org.mycompany.tracker.security.domain.User</class>
<class>org.mycompany.tracker.security.domain.UserGrantedAuthority</class>
<exclude-unlisted-classes/>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.flush_before_completion" value="false"/>
<property name="hibernate.connection.autocommit" value="/>
<property name="hibernate.generate_statistics" value="false" />
<property name="hibernate.archive.autodetection" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.hbm2ddl.import_files" value="/db/sql/load_test_users.sql"/>
</properties>
</persistence-unit>
</persistence>
User.orm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<named-query name="S_FIND_USER_BY_USERNAME" >
<query>SELECT user FROM User user WHERE user.username = :username</query>
</named-query>
</entity-mappings>
从user.username=:username的用户中选择用户
在为hibernate和spring启用跟踪的情况下调试应用程序时,我发现以下错误:
14:36:48 [main] org.hibernate.ejb.Ejb3Configuration.addXMLEntities (Ejb3Configuration.java:739) - HHH000360: Unable to resolve mapping file [/db/jpa/domain/User.orm.xml]
它出现在显示数据库信息之前:
name : MySQL
version : 5.6.10
major : 5
minor : 6
14:43:35 [main] org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure (JdbcServicesImpl.java:127) - Driver ->
name : MySQL Connector Java
version : mysql-connector-java-5.1.27 ( Revision: alexander.soklakov@oracle.com-20131021093118-gtm1bh1vb450xipt )
major : 5
minor : 1
当我将persistence.xml更改为:
<persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL">
<mapping-file>/db/jpa/domain/User.orm.xml</mapping-file>
/db/jpa/domain/User.orm.xml
到(在文件路径之前没有[/]):
db/jpa/domain/User.orm.xml
测试现在可以正常工作了
name : MySQL
version : 5.6.10
major : 5
minor : 6
14:43:35 [main] org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure (JdbcServicesImpl.java:127) - Driver ->
name : MySQL Connector Java
version : mysql-connector-java-5.1.27 ( Revision: alexander.soklakov@oracle.com-20131021093118-gtm1bh1vb450xipt )
major : 5
minor : 1
<persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL">
<mapping-file>/db/jpa/domain/User.orm.xml</mapping-file>
<persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL">
<mapping-file>db/jpa/domain/User.orm.xml</mapping-file>