@Column Specifies the mapped column for a persistent property or field. If no Column annotation is specified, then the filed names will be used for mapping.

package com.logicbig.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class ExampleEntity {
@Id
@Column(name = "example_id")
private String id;
@Column(name = "example_String")
private String myString;
@Column(name = "example_integer")
private int myInteger;
}
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-column-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 Main {
private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
"testPersistenceUnit");
private static final EntityManager entityManager = emf.createEntityManager();
public static void main(String[] args) {
//this will print column name, type, can-be-NULL,
// KEY and default value respectively
runNativeQuery("SHOW COLUMNS from ExampleEntity");
}
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 COLUMNS from ExampleEntity
[EXAMPLE_ID, VARCHAR(2147483647), NO, PRI, NULL]
[EXAMPLE_INTEGER, INTEGER(10), YES, , NULL]
[EXAMPLE_STRING, VARCHAR(2147483647), YES, , NULL]
Original PostDownload
Project Browser 