Close

JPA - @Transient Examples

JPA JAVA EE 

@Transient annotation is used to declare what instance variables cannot be persisted to database. Note that the keyword transient can also be used for that purpose. static fields are also not persisted.

The entity should not use final persistent instance variables. Specification doesn't allow that. Some existing JPA implementations are flexible on that and still support final fields, like in the following example.

package com.logicbig.example;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class MyEntity {
@Id
private String id;
@Transient
private String myString;
private transient int myInteger;
private static String aStaticString;
private final String aFinalString = "";
private volatile String aVolatileString;
private String anInstanceString;
}

src/main/resources/META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
</properties>
</persistence-unit>
</persistence>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.logicbig.example</groupId>
<artifactId>jpa-transient-annotation</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
</dependency>

<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.logicbig.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

public class ExampleMain {
private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
"testPersistenceUnit");
private static final EntityManager entityManager = emf.createEntityManager();

public static void main(String[] args) {
//h2 native query to show tables and columns
runNativeQuery("SHOW TABLES");
//this will print column name, type, can-be-NULL,
// KEY and default value respectively
runNativeQuery("SHOW COLUMNS from MyEntity");
}

private static void runNativeQuery(String s) {
System.out.println("--------\n" + s);
Query query = entityManager.createNativeQuery(s);
List list = query.getResultList();
for (Object o : list) {
System.out.println(Arrays.toString((Object[]) o));
}
}
}

Output

--------
SHOW TABLES
[MYENTITY, PUBLIC]
--------
SHOW COLUMNS from MyEntity
[ID, VARCHAR(2147483647), NO, PRI, NULL]
[AFINALSTRING, VARCHAR(2147483647), YES, , NULL]
[AVOLATILESTRING, VARCHAR(2147483647), YES, , NULL]
[ANINSTANCESTRING, VARCHAR(2147483647), YES, , NULL]
Original PostDownload Project Browser 




See Also