reset password
Author Message
skim144
Posts: 63
Posted 10:25 Jul 16, 2015 |

Hello professor,

When I tested your userDao example the test worked. I simply changed the code in your examples and implemented tests for my csjobs and ran it, and it showed me this.

 

Jul 16, 2015 10:10:28 AM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6eebc39e, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@464bee09, org.springframework.test.context.support.DirtiesContextTestExecutionListener@f6c48ac, org.springframework.test.context.transaction.TransactionalTestExecutionListener@13deb50e, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@239963d8]
[TestNG] Running:
  C:\Users\Sam\AppData\Local\Temp\testng-eclipse-1258413260\testng-customsuite.xml

Jul 16, 2015 10:10:28 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Jul 16, 2015 10:10:29 AM org.springframework.context.support.GenericApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@7a1ebcd8: startup date [Thu Jul 16 10:10:29 PDT 2015]; root of context hierarchy
Jul 16, 2015 10:10:30 AM org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
INFO: Building JPA container EntityManagerFactory for persistence unit 'springmvc'
Jul 16, 2015 10:10:30 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jul 16, 2015 10:10:30 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.19.Final}
Jul 16, 2015 10:10:30 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 16, 2015 10:10:30 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 16, 2015 10:10:30 AM org.hibernate.ejb.Ejb3Configuration configure
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: springmvc
    ...]
Jul 16, 2015 10:10:31 AM org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator instantiateExplicitConnectionProvider
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
Jul 16, 2015 10:10:32 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Jul 16, 2015 10:10:32 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Jul 16, 2015 10:10:33 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: springmvc.model.Reviewer
Jul 16, 2015 10:10:33 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: springmvc.model.Admin
Jul 16, 2015 10:10:33 AM org.hibernate.cfg.AnnotationBinder bindClass
WARN: HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: springmvc.model.Applicant
Jul 16, 2015 10:10:33 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Jul 16, 2015 10:10:33 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jul 16, 2015 10:10:34 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table Evaluation drop constraint FK_s6g5lmu0u2dp37q01hte556xf
Hibernate: alter table Evaluation drop constraint FK_9ks89ogmslrggokt5nc67caw9
Hibernate: alter table job_application_education_list drop constraint FK_l6b11v2bkqgti64r3ajtra0qj
Hibernate: alter table job_applications drop constraint FK_suqo4dnhrkf0w3spog62gopua
Hibernate: alter table job_applications drop constraint FK_740jd4s98demifcogu1atf7dn
Hibernate: alter table job_positions_applicants drop constraint FK_e6rupblxvjppstmsso3ujo69h
Hibernate: alter table job_positions_applicants drop constraint FK_b5fvxjtdh6f2mph2jp6213hr2
Hibernate: alter table job_positions_reviewers drop constraint FK_pt4jhavxbfypyq285byu9iuok
Hibernate: alter table job_positions_reviewers drop constraint FK_ry4ohpu5d9mmkj2d9715rofcx
Hibernate: drop table if exists Evaluation cascade
Hibernate: drop table if exists job_application_education_list cascade
Hibernate: drop table if exists job_applications cascade
Hibernate: drop table if exists job_positions cascade
Hibernate: drop table if exists job_positions_applicants cascade
Hibernate: drop table if exists job_positions_reviewers cascade
Hibernate: drop table if exists users cascade
Hibernate: drop sequence hibernate_sequence
Hibernate: create table Evaluation (id int4 not null, score int4 not null, application_id int4, reviewer_id int4, primary key (id))
Hibernate: create table job_application_education_list (job_application_id int4 not null, degree varchar(255), school varchar(255), year int4)
Hibernate: create table job_applications (id int4 not null, company varchar(255), title varchar(255), year varchar(255), applicant_id int4, jobPosition_id int4, primary key (id))
Hibernate: create table job_positions (id int4 not null, close_date timestamp, description varchar(255), publish_date timestamp, title varchar(255), primary key (id))
Hibernate: create table job_positions_applicants (job_position_id int4 not null, applicant_id int4 not null)
Hibernate: create table job_positions_reviewers (job_position_id int4 not null, reviewer_id int4 not null)
Hibernate: create table users (user_type varchar(31) not null, id int4 not null, email varchar(255) not null, first_name varchar(255), last_name varchar(255), password varchar(255) not null, city varchar(255), state varchar(255), street varchar(255), zipcode varchar(255), phone varchar(255), primary key (id))
Hibernate: alter table users add constraint UK_6dotkott2kjsp8vw4d0m25fb7 unique (email)
Hibernate: alter table Evaluation add constraint FK_s6g5lmu0u2dp37q01hte556xf foreign key (application_id) references job_applications
Hibernate: alter table Evaluation add constraint FK_9ks89ogmslrggokt5nc67caw9 foreign key (reviewer_id) references users
Hibernate: alter table job_application_education_list add constraint FK_l6b11v2bkqgti64r3ajtra0qj foreign key (job_application_id) references job_applications
Hibernate: alter table job_applications add constraint FK_suqo4dnhrkf0w3spog62gopua foreign key (applicant_id) references users
Hibernate: alter table job_applications add constraint FK_740jd4s98demifcogu1atf7dn foreign key (jobPosition_id) references job_positions
Hibernate: alter table job_positions_applicants add constraint FK_e6rupblxvjppstmsso3ujo69h foreign key (applicant_id) references users
Hibernate: alter table job_positions_applicants add constraint FK_b5fvxjtdh6f2mph2jp6213hr2 foreign key (job_position_id) references job_positions
Hibernate: alter table job_positions_reviewers add constraint FK_pt4jhavxbfypyq285byu9iuok foreign key (reviewer_id) references users
Hibernate: alter table job_positions_reviewers add constraint FK_ry4ohpu5d9mmkj2d9715rofcx foreign key (job_position_id) references job_positions
Hibernate: create sequence hibernate_sequence
Jul 16, 2015 10:10:36 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Jul 16, 2015 10:10:37 AM org.springframework.test.context.transaction.TransactionContext startTransaction
INFO: Began transaction (1) for test context [DefaultTestContext@63034ed1 testClass = UserDaoTest, testInstance = springmvc.model.dao.UserDaoTest@6e1ec318, testMethod = getUser@UserDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@232024b9 testClass = UserDaoTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@55a8dc49]; rollback [true]
Hibernate: select user0_.id as id2_6_0_, user0_.email as email3_6_0_, user0_.first_name as first_na4_6_0_, user0_.last_name as last_nam5_6_0_, user0_.password as password6_6_0_, user0_.city as city7_6_0_, user0_.state as state8_6_0_, user0_.street as street9_6_0_, user0_.zipcode as zipcode10_6_0_, user0_.phone as phone11_6_0_, user0_.user_type as user_typ1_6_0_ from users user0_ where user0_.id=?
Jul 16, 2015 10:10:38 AM org.springframework.test.context.transaction.TransactionContext endTransaction
INFO: Rolled back transaction for test context [DefaultTestContext@63034ed1 testClass = UserDaoTest, testInstance = springmvc.model.dao.UserDaoTest@6e1ec318, testMethod = getUser@UserDaoTest, testException = java.lang.NullPointerException, mergedContextConfiguration = [MergedContextConfiguration@232024b9 testClass = UserDaoTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
Jul 16, 2015 10:10:38 AM org.springframework.test.context.transaction.TransactionContext startTransaction
INFO: Began transaction (1) for test context [DefaultTestContext@63034ed1 testClass = UserDaoTest, testInstance = springmvc.model.dao.UserDaoTest@6e1ec318, testMethod = getUsers@UserDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@232024b9 testClass = UserDaoTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@55a8dc49]; rollback [true]
Jul 16, 2015 10:10:38 AM org.springframework.test.context.transaction.TransactionContext endTransaction
INFO: Rolled back transaction for test context [DefaultTestContext@63034ed1 testClass = UserDaoTest, testInstance = springmvc.model.dao.UserDaoTest@6e1ec318, testMethod = getUsers@UserDaoTest, testException = org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users], mergedContextConfiguration = [MergedContextConfiguration@232024b9 testClass = UserDaoTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
FAILED: getUser
java.lang.NullPointerException
    at springmvc.model.dao.UserDaoTest.getUser(UserDaoTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:200)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:171)
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:212)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:652)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:771)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1032)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

 

I got a nullpointer exception, I checked the table and all the data in my table got wiped out. (my sql files work fine).

Can you give me any suggestions?

 

Last edited by skim144 at 10:25 Jul 16, 2015.
cysun
Posts: 2935
Posted 10:36 Jul 16, 2015 |

Based on the log (starting at the line "INFO: HHH000227: Running hbm2ddl schema export"), it seems that you configured your project to a) run Hbm2ddl during build, and b) export the schema directly to the database instead of to a file. I would not recommend those, especially (b), because as you found out, it would wipe out all the data that's already in the database.

skim144
Posts: 63
Posted 10:50 Jul 16, 2015 |

Where can I check these configurations? Because I don't think I've done that.

cysun
Posts: 2935
Posted 11:09 Jul 16, 2015 |

Running Hbm2ddl during build requires binding it to a build cycle phase. It needs to be configured in pom.xml (and actually not very easy to do).

Exporting schema directly to database is controlled by the 2nd argument of the schemaExport.execute() method in the Hbm2ddl.java code.

skim144
Posts: 63
Posted 11:17 Jul 16, 2015 |

I do not have that configured in my pom.xml file.

I checked run configurations, and did not see such binding.

I even created new configuration and tried testing it, but it keeps on building hbm2dll file and executes create sql and drop sql.

Here is my pom.xml file:

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.csula.cs</groupId>
  <artifactId>csjobs</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <finalName>springmvc</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
              <descriptor>src/assembly/bin.xml</descriptor>
              <finalName>csjobs-src</finalName>
          </configuration>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>single</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
    
    </plugins>
  </build>
  <dependencies>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-webmvc</artifactId>
                      <version>4.1.6.RELEASE</version>
      </dependency>
      <dependency>
                      <groupId>org.hibernate</groupId>
                      <artifactId>hibernate-entitymanager</artifactId>
                      <version>4.2.19.Final</version>
      </dependency>
      <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-orm</artifactId>
                      <version>4.1.7.RELEASE</version>
      </dependency>
      <dependency>
                      <groupId>org.apache.tomcat</groupId>
                      <artifactId>tomcat-jdbc</artifactId>
                      <version>8.0.23</version>
      </dependency>
      <dependency>
                      <groupId>org.postgresql</groupId>
                      <artifactId>postgresql</artifactId>
                      <version>9.4-1201-jdbc41</version>
      </dependency>
      <dependency>
                      <groupId>org.testng</groupId>
                      <artifactId>testng</artifactId>
                      <version>6.9.4</version>
                      <scope>test</scope>
      </dependency>
      <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-test</artifactId>
                      <version>4.1.7.RELEASE</version>
                      <scope>test</scope>
      </dependency>
  </dependencies>
</project>

cysun
Posts: 2935
Posted 11:23 Jul 16, 2015 |

How did you run the tests?

skim144
Posts: 63
Posted 13:22 Jul 16, 2015 |

1. I deleted sql files and database connections

2. i updated maven project,

3. and i also deleted this from my persistence.xml: 

<property name="hibernate.hbm2ddl.auto" value="create" />

 

 

I don't know which of these solved the problem, but now my testing doesn't run sql files and is working properly.

<property name="hibernate.hbm2ddl.auto" value="create" />  This line of persistence.xml should only create, not run. Could this have been the problem?

cysun
Posts: 2935
Posted 13:26 Jul 16, 2015 |

Yes, #3 is the problem. "create" means creating database schema, which means running SchemaExport and exporting the schema to the database. In this case it actually has nothing to do with the Hbm2ddl.java program - it's Hibernate running SchemaExport internally.

skim144
Posts: 63
Posted 13:41 Jul 16, 2015 |

Got it. Thank you very much professor!!!