问题
线上出现了一个问题,跟hibernate相关,所以想先搭建一下环境测试
环境
用的idea旗舰版,很不好意思是破解的,本来是想买正版的,但是正式实在是太贵了。
新建项目Maven,使用SDK jdk 1.8
选择Create from archetype
, maven-archetype-webapp
项目
项目目录,如下
问题
问题1
写测试样例的时候,报错了
四月 25, 2017 8:15:35 下午 org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
信息: Discovered TestEngines with IDs: [junit-jupiter, junit-vintage]
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.AnnotationUtils.findAnnotatedMethods(Ljava/lang/Class;Ljava/lang/Class;Lorg/junit/platform/commons/util/ReflectionUtils$MethodSortOrder;)Ljava/util/List;
at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeAllMethods(LifecycleMethodUtils.java:41)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:88)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:77)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveClass(TestContainerResolver.java:98)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveElement(TestContainerResolver.java:47)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.tryToResolveWithResolver(JavaElementsResolver.java:164)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolve$8(JavaElementsResolver.java:155)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1548)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolve(JavaElementsResolver.java:158)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolveForAllParents$4(JavaElementsResolver.java:128)
at java.util.Collections$SingletonSet.forEach(Collections.java:4767)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveForAllParents(JavaElementsResolver.java:127)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveContainerWithParents(JavaElementsResolver.java:78)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveMethod(JavaElementsResolver.java:64)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolveSelectors$3(DiscoverySelectorResolver.java:63)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:62)
at org.junit.jupiter.engine.JupiterTestEngine.resolveDiscoveryRequest(JupiterTestEngine.java:50)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:109)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:79)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
于是在项目根目录去命令行mvn test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.512 s
[INFO] Finished at: 2017-04-25T21:05:02+08:00
[INFO] Final Memory: 13M/197M
[INFO] ------------------------------------------------------------------------
也就是测试没有识别到test样例
最后才发现是导入的jar包有冲突
<!-- <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency> -->
这个jar 跟 junit是冲突的,注释掉就可以了
问题2
Caused by: org.hibernate.MappingException: Could not determine type for: String, for columns:
<property name = "name" column="name" type = "string"/>
type指的是hibernate中的类型,不是java中的String类型,必须小写。
问题3
Cannot resolve reference to bean 'txPointCut' while setting bean property 'pointcut';
在声明式事务管理器,txPointCut一直报错
换了jar包,还是不行
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
最后做法,把target里面的lib的包都删了,重新build项目,就ok了,应该之前导入的一些Jar包冲突,修改了pom.xml,并没有自动删除掉
问题4
org.hibernate.hql.internal.ast.QuerySyntaxException: Tuser is not mapped [FROM Tuser]
user.hbm.xml配置里面其实是有Tuser表的映射的,通过listener把上下文的配置环境加载了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-hibernate.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而在spring-hibernate.xml又有标签
<property name="mappingLocations">
<list>
<value>classpath:/user.hbm.xml</value>
</list>
</property>
把user.hbm.xml的实体类加载了
最后发现hql语句中的表名应该用ORM映射出来的类名User,而不是数据库中的表名Tuser
select * from 表名(应该用类名)
问题5
hibernate4版本过后,实体类的注解@Table中不能含有name="表名",只能@Entity(name="类名")
问题6
单元测试,需要如下Jar包,否则报错
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
最后变成了怎么启动都启动不了,最后的最后,盗了网上的一份pom.xml就好了,应该又是jar冲突的问题
自动化测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring-hibernate.xml")
@TransactionConfiguration(defaultRollback=true)
放一些这些注解,把一些xml导入进去,还有测试完成后,数据库需要清空一下,下载链接如下
test_web
深copy
把需要copy的类 public class classname
implements Serializable 实现序列化这个接口
public Object deepClone()
{
//将对象写到流里
ByteArrayOutputStream bo=new ByteArrayOutputStream();
ObjectOutputStream oo=new ObjectOutputStream(bo);
oo.writeObject(this);
//从流里读出来
ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi=new ObjectInputStream(bi);
return(oi.readObject());
}
这里面有个坑,就是如果这个classname
里面有个属性是非基本类型比如property
类,那么proerty
类也要implemnts Serializable,不然也得跪
这里面因为牵扯到io了,所以有checked Exception需要往上抛
线上问题
因为某个属性(也是类)牵扯到了hibernate里面的东西,虽然实现了深copy,但是赋值session值出问题了,所以使用gson会报错