当我在 tomcat 服务器上运行我的 Web 应用程序时,我的命名查询已正确加载:
但是当我使用 MockMvc 休眠时找不到任何命名查询。结果我在执行测试时出错
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}
测试日志:
12:36:51 [main] org.hibernate.tool.hbm2ddl.SchemaExport.execute (SchemaExport.java:343) - HHH000227: Running hbm2ddl schema export
12:36:51 [main] 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:36:51 [main] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists user
Hibernate: drop table if exists user
12:36:51 [main] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists user_authority
Hibernate: drop table if exists user_authority
12:36:51 [main] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - drop table if exists hibernate_sequence
Hibernate: drop table if exists hibernate_sequence
12:36:51 [main] 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:36:51 [main] 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:36:52 [main] 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:36:52 [main] 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:36:52 [main] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - create table hibernate_sequence ( next_val bigint )
Hibernate: create table hibernate_sequence ( next_val bigint )
12:36:52 [main] org.hibernate.SQL.logStatement (SqlStatementLogger.java:104) - insert into hibernate_sequence values ( 1 )
Hibernate: insert into hibernate_sequence values ( 1 )
12:36:52 [main] org.hibernate.tool.hbm2ddl.SchemaExport.execute (SchemaExport.java:405) - HHH000230: Schema export complete
12:36:52 [main] org.hibernate.internal.SessionFactoryImpl.checkNamedQueries (SessionFactoryImpl.java:1070) - Checking 0 named HQL queries
12:36:52 [main] org.hibernate.internal.SessionFactoryImpl.checkNamedQueries (SessionFactoryImpl.java:1093) - Checking 0 named SQL queries
12:36:52 [main] org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService (AbstractServiceRegistryImpl.java:147) - Initializing service [role=org.hibernate.service.config.spi.ConfigurationService]
12:36:52 [main] org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService (AbstractServiceRegistryImpl.java:147) - Initializing service [role=org.hibernate.stat.spi.StatisticsImplementor]
12:36:52 [main] org.hibernate.stat.internal.StatisticsInitiator.initiateServiceInternal (StatisticsInitiator.java:110) - Statistics initialized [enabled=false]
12:36:52 [main] org.hibernate.ejb.metamodel.MetadataContext.wrapUp (MetadataContext.java:183) - Wrapping up metadata context...
持久性.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>
MycompanySecurityTest.java:
package org.mycompany;
import com.google.common.base.Preconditions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.Resource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration()
@ActiveProfiles("test")
@ContextConfiguration(locations = {
"classpath:rootContext.xml",
"classpath:servletContext-mobile.xml"
})
public class MycompanySecurityTest {
@Resource
protected WebApplicationContext wac;
protected MockMvc mockMvc;
public static final String VALID_USER_AUTH = "test:test";
@Resource
private FilterChainProxy springSecurityFilter;
public static final String HEADER_AUTHORIZATION = "Authorization";
@Before
public void before(){
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
.defaultRequest(post("/")
.servletPath("/m")
.accept(MediaType.APPLICATION_JSON)
)
.addFilter(this.springSecurityFilter)
.build();
}
@Test
public void shouldReturnUnauthorizedErrorWithEmptyBodyAuthenticateRealmWhenAuthorizationHeaderNotSet() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/m/sync"))
.andDo(print())
.andExpect(status().isUnauthorized())
.andExpect(content().string(""))
.andReturn();
assertEquals("Full authentication is required to access this resource", mvcResult.getResponse().getErrorMessage());
}
@Test
public void thatDataCanBeSyncedByProperUser() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/m/sync")
.header(HEADER_AUTHORIZATION, composeAuthenticationHeaderValue(VALID_USER_AUTH)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$trackers[0].points[0].type").value("gps"))
.andReturn();
assertNull(mvcResult.getResponse().getErrorMessage());
}
@Test
public void thatDataCanNotBeSyncedByIvalidUser() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/m/sync")
.header(HEADER_AUTHORIZATION, composeAuthenticationHeaderValue("invalid-user:invalid-pass")))
.andDo(print())
.andExpect(status().isUnauthorized())
.andExpect(content().string(""))
.andReturn();
assertEquals("Bad credentials", mvcResult.getResponse().getErrorMessage());
}
@Test
public void thatErrorReturnedIfInvalidTokenFormatSpecified() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/m/sync")
.header(HEADER_AUTHORIZATION, composeAuthenticationHeaderValue("user-user")))
.andDo(print())
.andExpect(status().isUnauthorized())
.andExpect(content().string(""))
.andReturn();
assertEquals("Invalid basic authentication token", mvcResult.getResponse().getErrorMessage());
}
private static String encodeString(String str) {
Preconditions.checkNotNull(str);
return new String(Base64.encode(str.getBytes()));
}
private static String composeAuthenticationHeaderValue(String auth){
return "Basic " + encodeString(auth);
}
}
用户.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>