Note that there are some explanatory texts on larger screens.

plurals
  1. POMockMvc does not loads named query
    primarykey
    data
    text
    <p>When I run my web application on tomcat server my named query is loaded properly:</p> <p>But when I use MockMvc hibernate cannot find any named queries. As a result I have en error when executing tests</p> <pre><code>MockHttpServletResponse: Status = 401 Error message = Named query not found: S_FIND_USER_BY_USERNAME </code></pre> <p>Tomcat log:</p> <pre><code>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 &lt;&lt; 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) -&gt; 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 -&gt; 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 -&gt; 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) -&gt; 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 -&gt; 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 -&gt; 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 &gt;&gt; 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} </code></pre> <p>Test log:</p> <pre><code>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... </code></pre> <p>persistence.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL"&gt; &lt;mapping-file&gt;/db/jpa/domain/User.orm.xml&lt;/mapping-file&gt; &lt;class&gt;org.mycompany.tracker.security.domain.User&lt;/class&gt; &lt;class&gt;org.mycompany.tracker.security.domain.UserGrantedAuthority&lt;/class&gt; &lt;exclude-unlisted-classes/&gt; &lt;properties&gt; &lt;property name="hibernate.show_sql" value="true"/&gt; &lt;property name="hibernate.transaction.flush_before_completion" value="false"/&gt; &lt;property name="hibernate.connection.autocommit" value="/&gt; &lt;property name="hibernate.generate_statistics" value="false" /&gt; &lt;property name="hibernate.archive.autodetection" value="false" /&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/&gt; &lt;property name="hibernate.id.new_generator_mappings" value="true"/&gt; &lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt; &lt;property name="hibernate.hbm2ddl.import_files" value="/db/sql/load_test_users.sql"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>MycompanySecurityTest.java:</p> <pre><code>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); } } </code></pre> <p>User.orm.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;named-query name="S_FIND_USER_BY_USERNAME" &gt; &lt;query&gt;SELECT user FROM User user WHERE user.username = :username&lt;/query&gt; &lt;/named-query&gt; &lt;/entity-mappings&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload