How to use JPA annotations in ormlite? - jpa

I would like to use JPA annotations in ormlite. These annotations (like #Entity or #Id) belong to the javax.persistence package which is apparently not provided with ormlite. I could not get JPA annotations resolved, whereas ormlite specific annotations (like #DatabaseTable) are ok. Do I need to download a third party jar in order to get JPA annotations working in ormlite ?
I need to work with ormlite + JPA only.
When having a look at http://ormlite.com/javadoc/ormlite-core/index-all.html we can see that there are no JPA annotations documented nor available, although they are described in the user manual.
Thanks a lot in advance !!!

javax.persistence is available in a jar from the central maven repository (for example). It contains all annotations as well as all of the other JPA stuff that ORMLite ignores.

CMobileCom JPA is a new implementation of JPA specification for both Java JDBC and Android. It is light-weight, about 380K. You can add the following gradle dependencies:
Android:
dependencies {
implementation("com.cmobilecom:cmobilecom-jpa-android:${version}#aar") {
transitive = true
}
annotationProcessor "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
Java JDBC:
dependencies {
implementation "com.cmobilecom:cmobilecom-jpa-jdbc:$version"
// annotation processor: generate static metamodel
compileOnly "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
JPA annotations are supported, so you do not need to add another jar to resolve JPA annotations in your code.
See Developer Guide:
https://cmobilecom.com/docs/jpa/latest/devguide/index.html
Disclaimer: I am a developer of CMobileCom JPA.

Related

liquibase and jpa buddy using gradle

I'm trying to use JPA Buddy to generate changelogs from my entities.
In this answer I can see the Liquibase option:
https://stackoverflow.com/a/66516541/6047129
But in my IDE thats all I got:
I'm missing some gradle or liquibse configuration?
Ok, I had liquibase dependency declared as:
liquibaseRuntime 'org.liquibase:liquibase-core:4.11.0'
As JPA Buddy scans the project dependencies for possible integrations, I needed to change the liquibase dependency to:
implementation 'org.liquibase:liquibase-core:4.11.0'

How to load selected beans from external library in micronaut

I'm used to creating custom Spring Boot based libraries with a number of different beans per library. A target Spring Boot app then would use #Import to make a subset of those Beans available w/out importing all of the beans from an external library. An external library would also include spring integration tests (i.e. #SpringBootTest).
I'm trying to replicate this functionality in Micronaut. I've created an external library using 'io.micronaut.library' gradle plugin. It also contains some micronaut integration tests (i.e. #MicronautTest).
Per https://docs.micronaut.io/latest/guide/#beanImport I expected none of the beans to be imported by default. However, I've noticed that a class annotated with #Singleton was imported by default. I suspect this is due to its inclusion in the META-INF/services/io.micronaut.inject.BeanDefinitionReference within the library jar. If I remove META-INF/services/io.micronaut.inject.BeanDefinitionReference from the jar manually then none of the beans are imported by default and I can use explicit #Import to import what is needed.
The package of the library is com.some.micronaut.http while application package is com.some.somethingelse so the namespaces do not match. Both the library and the application are written in Kotlin.
Is there a way to control which beans are imported from an external library? Or can I control which bean references are included in META-INF/services/io.micronaut.inject.BeanDefinitionReference?
Here is a relevant build.gradle portion from the external library:
plugins {
id("org.jetbrains.kotlin.jvm") version "${kotlinVersion}"
id("org.jetbrains.kotlin.kapt") version "${kotlinVersion}"
id("org.jetbrains.kotlin.plugin.allopen") version "${kotlinVersion}"
id 'io.micronaut.library' version "3.1.1"
}
dependencies {
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")
//test
testImplementation 'io.micronaut.test:micronaut-test-junit5'
testImplementation 'org.junit.jupiter:junit-jupiter-params'
//wiremock for http client tests
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.32.0'
}
I'm flexible about usage of io.micronaut.library plugin but I do need to be able to run #MicronautTest as part of the library build.
For now, I've removed META-INF/services/io.micronaut.inject.BeanDefinitionReference from the library jar explicitly in my build.gradle:
jar {
exclude('META-INF/services/io.micronaut.inject.BeanDefinitionReference')
}
and then used #Import in my application to explicitly import the desired components. I've also submitted an enhancement request.

OSGI Bundle implementing JPA Using DataNucleus 4.0.0 Release

I am trying to implement JPA using DataNucleus in an OSGI Environment (Apache ServiceMix). I am following the guide mentioned here - http://www.datanucleus.org/products/datanucleus/jpa/osgi.html (Section under Name "JPA and OSGi")
As per the documentation here I am using the DataNucleus JPA jar. So I have Deployed this Jar on ServiceMix as follows -
osgi:install mvn:org.datanucleus/datanucleus-jpa/2.1.7
osgi:start bundleId
As this jar export the persistence provider,I have correctly changed the provider in my persistence.xml file to be org.datanucleus.jpa.PersistenceProviderImpl (which is exported by the above bundle) rather than usual org.datanucleus.api.jpa.PersistenceProviderImpl which is in a normal J2EE JPA APP.
I also have the DataNucleus Core Bundle running on my ServiceMix, which was installed on my ServiceMix using the following -
osgi:install mvn:org.datanucleus/datanucleus-core/4.0.0-release
osgi:start bundleId
Please Note: I donot have the datanucleus-api-jpa bundle running on my ServiceMix, as per the documentation, the datanucleus-jpa bundle would export the necessary classes and hence datanucleus-api-jpa bundle would not be required.
At runtime, I am getting the following error Caused by: java.lang.ClassNotFoundException: org.datanucleus.PersistenceConfiguration not found by org.datanucleus.jpa [261]
What I have noticed is that the DataNucleus Core version 4.0.0-release does not have this class, but this class exists in all previous releases (3.2.15).
I cannot deploy DataNucleus Core Version 3.2.XX onto my ServiceMix, because I have other bundles using DataNucleus Core 4.0.0-release and having both is not an option because of Singleton Creation problems.
Please let me know, if you need me to post my code, I will do so.
Please help
Cheers,
Abhijit
In conclusion, you are using incorrect/invalid jars. "datanucleus-jpa" was removed back in v2 of DataNucleus. All recent versions using "datanucleus-api-jpa" (or "datanucleus-api-jdo" if using JDO). A simple download of the DataNucleus zip file for 4.0.x would show this very clearly.

IntelliJ IDEA highlights #Entity class names with "Cannot resolve symbol" in JPQL

IntelliJ IDEA highlights persistent #Entity class names with "Cannot resolve symbol" in red in JPQL which is distracting and buries real issues.
So, for example, I declare a query in my repository:
private static final String READ_BY_CANDIDATE_KEY = "SELECT cr FROM Entity AS cr left join cr.relationship AS re left join fetch cr.relationship2 WHERE re.candidateKey=:ID";
.. and "Entity" is underlined, even though "Entity" is a valid class name, and has the #Entity annotation. When the code actually runs, there are no problems.
I imagine some sort of configuration is required to let the IDE know what classes are valid? How is that configuration done?
Update: I do have a JPA facet, but it doesn't see the annotated classes. It seems to require a persistence.xml or orm.xml (which my project does not use)
Seems like you have not selected the default JPA provider in facet configuration. Depending upon which provider you are using, pick one from the list. Available options are EclipseLink, Hibernate, OpenJPA, TopLink
Make sure you have JPA or Hibernate facet configured in IntelliJ IDEA for your module.
If you are using Spring Boot with maven, add this dependency in your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
File -> Project Structure
At left pane select "Facets". If there is no JPA listed, click "+" sign and add "JPA"
At bottom of same dialog, at "Default JPA Provider", select - "Hibernate", press "OK"
If you have error at #Table annotation, configure and choose data source
Table name for select now should be recognized as entity class name
you would have missed this dependency- Spring data JPA
This one is for Maven projects
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
add this one in your POM under dependencies section
and then use ctrl+click on #Entity to import it from
import javax.persistence.Entity;
for Gradle follow the same and use
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
under dependencies in the build.gradle file
I ran into the same symptom as the OP (IntelliJ highlights entity in JPQL with the error "Cannot resolve symbol") but the solution turned out to be invalidating the IntelliJ caches and restarting the IDE.
Try adding this dependency if you are using Spring-boot.
spring-boot-starter-data-jpa
Sometimes JPA Buddy plugin brakes the springboot JPA configuration. Make sure this is not causing the issue.

Trouble with Aspectj load-time-weaving when using EclipseLink JPA in Spring dm Server 1.x

I am attempting to get EclipseLink JPA working inside the Spring dm Server OSGi environment.
Relevant frameworks and libraries downloaded from the Spring Enterprise Bundle Repository include:
dm Server 1.0.2.SR02
AspectJ Runtime 1.6.3
AspectJ Weaver 1.6.3
Spring Framework 2.5.6.A
Eclipse Persistence 1.1.0
Javax Persistence API 1.99.0
I followed the same structure as presented in the PetClinic-1.5.0 example for setting up EclipseLink JPA. Everything works until lazy-fetching is enabled (which requires proxied objects).
Once lazy-fetching is enabled, the following error suggests that load-time-weaving is not working correctly.
---- (truncated for readability)
Exception [EclipseLink-60] (Eclipse Persistence Services - 1.1.0.r3634): org.eclipse.persistence.exceptions.DescriptorExcep tion
Exception Description: The method [_persistence_setcustomer_vh] or [_persistence_getcustomer_vh] is not defined in the object [net.fractech.fds.backoffice.Job].
Internal Exception: java.lang.NoSuchMethodException: net.fractech.fds.backoffice.Job._persistence_getcu stomer_vh()
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[customer]
Descriptor: RelationalDescriptor(net.fractech.fds.backoffice.J ob --> [DatabaseTable(JOBS)])
This shows that the _persistence_getcustomer_vh() and _persistence_setcustomer_vh() methods were not automatically weaved into the Job domain object.
Questions
1.) How do I determine if load-time-weaving is actually working; moreover, how do I log which load time weaving agent and weaver was started? How do I pass switches to this weaver to have it output debugging information?
I assume I started load-time-weaving with <context:load-time-weaver aspectj-weaving="on" />
2.) Many searches have revealed that I do not need to pass the -javaagent parameter to the jvm when using dm Server. Is this correct?
3.) I have assured that my domain objects in another bundle have access to the eclipse persistence classes by asserting com.springsource.org.eclipse.persistence;version="[1.1.0,1.1.0]";import-scope:=application in my eclipselink bundle and including all application bundles within a PAR. Are there any other configurations needed to enable EclipseLink JPA in Spring dm Server?
I had similar problems. First try setting eclipselink.weaving.lazy=false, or eclipselink.weaving=false if that doesn't work. I had to set the latter.
If you would like to refer to the setup I am using to see if it applies to you, I have a post about it on my site.
It is better to use Equinox Waving Springwaver
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
<property name="loadTimeWeaver">
<bean class="org.eclipse.equinox.weaving.springweaver.EquinoxAspectsLoadTimeWeaver"/>
</property>
</bean>
You don't need to use -javaagent option then.
You can find working examples with JPA and EclipseLink here http://code.google.com/p/springdm-in-action/ (see Chapter 7).
I have tried to use EquinoxAspectsLoadTimeWeaver into JPa context (with EclipseLink) but it doen't transform the model classes if your EquinoxAspectsLoadTimeWeaver bean declaration is not done into the same bundle than Model bundle.
EquinoxAspectsLoadTimeWeaver transform class ONLY for the classes stored into the bundle wich declare EquinoxAspectsLoadTimeWeaver.
I have tried the sample http://code.google.com/p/springdm-in-action/ (see Chapter 7) (thank for this sample Lukasz). The declaration of EquinoxAspectsLoadTimeWeaver avoid having the error
Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
But Model classes are not transformed (woven). Weaving into EclipseLink manage for instance lazy mode. For instance if you set into the sample model Contact lazy mode like this :
public class Contact {
...
#Column(name="last_name")
#Basic(fetch=FetchType.LAZY)
private String lastName;
you will notice that lazy loading is not applied because Model Contact class is not wowen.
Regards Angelo