Facing Spring Roo issue while converting JPA project to non-JPA project - mybatis

I have a Spring - JPA project generated using Spring Roo. Now for some requirement we are replacing our JPA layer with some other framework (MyBatis) which is not JPA-compliant. I have done the changes and they are working fine also. I have removed all the JPA dependencies from my pom.xml file.
I am facing issues with my JUnit test project which is used to test DAO layer. Spring Roo is looking for the javax.persistence.Entity class which is part of JPA specific jar.
Please find below the error details below:
can't determine annotations of missing type javax.persistence.Entity
when weaving type ****.**.***Test
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=12 AopXmls=#0
[Xlint:cantFindType]
error at (no source information available
Can anyone please advise me how to remove this error without adding a JPA dependency?

You could try to make a push-in from your test .aj file to the related .java file and then remove the necessary code (imports, annotations) that makes that your Spring Roo shell shows that error.
If you are not sure about how to make a push-in, you could read Spring Roo 1.3.2.RELEASE documentation http://docs.spring.io/spring-roo/docs/1.3.2.RELEASE/reference/html/removing.html#removing-step-by-step-1
Regards,

Related

Does Spring Data JPA internally use Hibernate & why my app is working if I am not giving dialect property?

I just started learning Spring Data JPA, I connected to mysql in localhost and able to save a record but I am unable to understand why it is working if I am not giving dialect property in properties file and is hibernate a default implementation of spring data instead of ibatis or Eclispe link, because in my pom.xml I just added the dependency of spring-data-jpa and never mentioned what kind of JPA implementation I want to use.
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.
spring.datasource.url=jdbc:mysql://localhost:3306/initsoftware
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=ppppppp
spring.datasource.password=xxxxxxx
logging.level.root=DEBUG
spring.jpa.show-sql=true
Since you have an application.properties I assume you are using Spring Boot and not just Spring Data JPA.
In order to use JPA with Spring Boot you would typically add spring-boot-starter-data-jpa to your dependencies. This indeed comes with Hibernate out of the box as you can see when you inspect the dependencies.
Spring Data JPA itself doesn't come with a JPA implementation. You have to add that.
iBatis is not a JPA implementation.
If the assumption above doesn't match your scenario you can use the maven dependency plugin to inspect your (transient) dependencies. The following is a good starting point.
mvn dependency:tree -Dverbose
If you use a different build tool, it probably has a similar feature.

Packaging JPA entities in a jar inside a Spring Boot application

I am refactoring a JEE REST (using JAX-RS 2.0) application as a Spring Boot application. My old app is packaged in a .war and has a jar file with entities and the persistence.xml configuration file for JPA. This jar is copied into WEB-INF/lib directory. I know Spring JPA works a different way and I don't use persistence.xml now but I wonder if I can package my JPA entity classes in a jar and include them in my Spring Boot apps just like I am doing now. This way I can easily reuse that jar in different Spring Boot Applications.
I'm pretty certain you can do this since I have done the same on one of my projects very recently. The only thing you need to do is make sure that you add an #EntityScan annotation on your main Spring Boot config class with the base package of your entities in the JAR.
#EntityScan("my.external.jar.entity.package")
Spring Boot doesn't really care whether the JPA entities are packages as a separate jar or included into the application. Its a runtime framework and in runtime classes can be loaded from the jar (it should reside in BOOT-INF/lib or 'directly' from the *.class files in the spring boot artifact.
Now there is a rule in spring boot, that says that it will scan for beans (including entities) only in the package where your "main" class resides or under it. This is done in order to avoid long process of analysis of, say, third-party classes that you might use. These third-party classes are usually not spring aware at all, at certainly do not contain any spring beans.
Example:
Say, you place your "main" class (the one annotated with #SpringBootApplication) in the package: com.mycompany.myapp
In this case, the following packages will be scanned (just a couple of examples):
com.mycompany.myapp
com.mycompany.myapp.web
com.mycompany.myapp.services.bl
com.mycompany.myapp.whatever.doesnt.matter
...
The following packages won't be scanned however (again, examples, not the full list):
com.mycompany
com.anothercompany
org.hibernate
If you want to to "alter" this default rule and place the entities in the package that doesn't adhere this convention, for example com.mycompany.jpa.entities then you should indeed use #EntityScan annotation as our colleagues have already suggested.
You can read about this topic here. You might also need to get familiar with #EnableJpaRepositories if you're using spring data but, while related, its a different topic.
In my case I had this problem, and after importing the library in the application's pom.xml, in the SpringBoot Project Main class, insert an #EntityScan annotation with the first package and *. Like this: #EntityScan ("br.*")

Walkthrough the spring source code

I wanted to walkthrough the spring code. For that I cloned the code and built it using ./import-into-eclipse.sh command. There are some instructions given in README.md file for checking out source in eclipse.
After following above instructions I was able to see the spring code in my eclipse workspace. To refer the spring source from my project I added all of spring projects (like spring-beans, spring-aop etc) in my project's Java build path. I was able to use spring code in my project.
Now the issue is that whenever I run the test cases in my project, I get initialization error. I removed spring projects from my java build path and junit test started working.
I tried to dig into the issue. For that I tried to run some of the test cases of spring projects itself like BeanUtilsTests.
but I got:
Class not found org.springframework.beans.BeanUtilsTests
java.lang.ClassNotFoundException: org.springframework.beans.BeanUtilsTests
I verified that classpath is set:
Don't know what is the issue. Can someone please tell me that what I did wrong.
Is there any better way to view/edit spring code?
Usually the below error,
Class not found org.springframework.beans.BeanUtilsTests
java.lang.ClassNotFoundException: org.springframework.beans.BeanUtilsTests
occurs when the corresponding jar is missing in the classpath. Since you have already added the spring-beans jar manually, I suspect you are using an incompatible version of the spring-beans jar with your spring-boot.
You can check the Maven repo Spring Beans to check the compatible version. But I would suggest using a dependency management tool like Gradle or Maven to include the required dependency jars. Spring-Boot Gradle file

How to explain the different behaviour of spring in eclipse and standalone application

We have a web application using spring framework which works well. We try to use the same spring context in a standalone application built with maven. We managed to get the context by launching the application within eclipse, a main class instantiates the Spring context with the following lines:
ApplicationContext ap = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
But when we build a jar with maven and launch it with a command line we encounter errors. Here is the command line:
java -jar application.jar
The jar contains a META-INF that defines the classpath generated by maven-jar-plugin.
First spring failed on this error
Unable to locate NamespaceHandler when using context:annotation-config
We solved the problem by merging the different spring.handler files from maven spring jars and putting the result in our application.jar. This is a common solution to this problem but this doesn't explain why this failed in standalone mode but worked in a web app and in eclipse. Eclipse and standalone use the same code (java and spring configuration files).
Now we are facing a second problem, some beans instantiations fail. Some of our beans are declared with XML, some other beans are declared with annotations. Beans declared with annotations are not instantiated in standalone mode, traces in constructors show it, so instantiation of XML beans referencing these beans fails. We didn't solve this problem yet.
We don't understand why we observe a different behavior in eclipse or in standalone mode. What can explain the difference ? How does eclipse call java, how does-it set the classpath ?
Thanks for any response,
Mickaƫl

javax.persistence.PersistenceException

i have a problem with a JPA project that is connected with a EE project, i have done the tests (JUnit 4) for the DAOs in a source package with a persistence.xml only for this test units.
when i'm going to test them they get me this error, like if the unit tests can not found the persistence provider.
please some one help me.
thank you
Persistence Provider can't be found refers to JPA provider Jar file not being included into your test environment. Make sure JPA provider (Hibernate/ OpenJPA or whatever is provided with your app server) jar is available in classpath from where your test case reads dependent libraries.