Close

JPA - @Entity Examples

JPA JAVA EE 



The persistence class must have @Entity annotation. By default an entity name is the unqualified name of the class. The entity class must also have a field with @Id which corresponds to a primary key in the table. Without @Id we will have an exception during load time.

In the following example we are using maven, embedded H2 database and the JPA implementation EclipseLink. We are using H2 native queries to see what table/columns are created.

Note that, in persistence.xml, the value of javax. persistence. schema-generation. database. action is specified as 'create', that means table are generated automatically during startup time.

package com.logicbig.example;

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

@Entity
public class MyEntity {
@Id
private String id;
}

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-entity</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");
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]
Original PostDownload Project Browser 




Instance variables to column mapping Example

This example shows that, by default, instance variables of the entity class will be mapped to the table columns having same name as variables.

Note that if column names need to be different then we can use @Column to specify a different name.

package com.logicbig.example;

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

@Entity
public class MyEntity {
@Id
private String id;
private String myString;
private int myInteger;
}

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]
[MYINTEGER, INTEGER(10), YES, , NULL]
[MYSTRING, VARCHAR(2147483647), YES, , NULL]
Original PostDownload Project Browser 




package com.logicbig.example;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EntityA {
@Id
@GeneratedValue
private int id;

@Embedded
private ClassA classARef;

public ClassA getClassARef() {
return classARef;
}

public void setClassARef(ClassA classARef) {
this.classARef = classARef;
}

@Override
public String toString() {
return "EntityA{" +
"id=" + id +
", classARef=" + classARef +
'}';
}
}
Original Post




See Also