Maven Learning (2): maven Learning

Source: Internet
Author: User

Maven Learning (2): maven Learning

The m2eclipse plug-in was installed on eclipse in the evening, and then a small project was created using maven for testing. However, several problems occurred during the test. Now, let's briefly summarize:

The project is to add hibernate support, and then use junit to test and add a piece of data.

The project structure is as follows:

You may wonder why the hibernate configuration file is included in the resource package,

This is what I want to talk about.First question:If no hibernate configuration file exists in the test resource package during junit testing, maven reports an error saying that the hibernate configuration file cannot be found..

As follows:

java.lang.ExceptionInInitializerError    at lizm.pro.user.dao.UserDao.<init>(UserDao.java:11)    at lizm.pro.user.dao.TestUserDao.testAdd(TestUserDao.java:13)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:606)    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:606)    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2095)    at org.hibernate.cfg.Configuration.configure(Configuration.java:2076)    at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)    at lizm.pro.user.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)    at lizm.pro.user.util.HibernateUtil.<clinit>(HibernateUtil.java:10)    ... 31 more

I asked Yao Niang what the best thing du Niang gave.The solution is to add the hibernate configuration file to the test resource package..

 

 

Question 2: mysql package version Problems

After the author finally solved the problem that he could not find the hibernate configuration file, he thought that he could play with maven happily. Who knows that there was another problem, the second problem is like this:

Hibernate: insert into _user (cnname, passWord, userName) values (?, ?, ?)org.hibernate.exception.GenericJDBCException: could not execute statement    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)    at lizm.pro.user.dao.UserDao.add(UserDao.java:19)    at lizm.pro.user.dao.TestUserDao.testAdd(TestUserDao.java:15)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:606)    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:606)    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)Caused by: java.sql.SQLException: Could not retrieve transation read-only status server    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:904)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:894)    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3613)    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3582)    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2111)    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2081)    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2066)    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)    ... 52 moreCaused by: java.sql.SQLException: Unknown system variable 'tx_read_only'    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2531)    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2489)    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3607)    ... 57 more

The SQL statement came out, but the test was not successful. At first, I thought it was a wrong transaction commit, but it was a JDBC exception. It must have nothing to do with the transaction, so I continued to ask du Niang What the exception was, and then I was able to see the answers from many great gods:Package version ProblemsAs soon as I checked my mysql package, I went there. It was the latest, more than 5 o'clock. How can this problem be solved? I wonder if my problem is different from other people's. I changed the mysql package to more than three o'clock with a try. I tried it successfully.

When I started using maven, I was excited to use the latest JAR package. Now I think it's really not the case. It's right to use the new version, but the stable version may be better, soYou don't need to use the latest version in the future, but you also need to use the latest version as much as possible..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.