Question

I am new to JPA, I have created a class like the following

/**
 * 
 */
package programme;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author anoop
 *
 */
@Entity
@Table(name="course")
@Access(AccessType.FIELD)
public class programme {
    @Id private int id;
    private String name;
    private int year;
    @Transient private String comments;

    //getters and setters for the fields.
}

I noted that there is another class with name as my entity java class Programme.java with an underscore i.e. Programme_.java. What is this class and why is this generated. Is there any way that I can stop its generation?

the code for the Programme_.java class is as following.

package programme;

import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;

@Generated(value="Dali", date="2014-04-27T21:32:59.433+0530")
@StaticMetamodel(programme.class)
public class programme_ {
    public static volatile SingularAttribute<programme, Integer> id;
    public static volatile SingularAttribute<programme, String> name;
    public static volatile SingularAttribute<programme, Integer> year;
    public static volatile SingularAttribute<programme, String> comments;
}
Was it helpful?

Solution

This is your "Canonical Metamodel". It can be used with the JPA Criteria API, as described by the JPA 2.x spec (Section 6.2).

To stop Eclipse from generating these classes: edit your Eclipse project properties > JPA > Canonical metamodel (JPA 2.0) > Source folder - set the folder's value to "".

OTHER TIPS

This is the change I made to persistence.xml to prevent a Java file and a class file from being generated with an underscore (which was causing me a problem):

diff --git a/examples/javaee7/src/main/resources/META-INF/persistence.xml b/examples/javaee7/src/main/resources/META-INF/persistence.xml
index 86d3c0c..a477432 100644
--- a/examples/javaee7/src/main/resources/META-INF/persistence.xml
+++ b/examples/javaee7/src/main/resources/META-INF/persistence.xml
@@ -1,9 +1,9 @@
 <?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="javaee7addressbookPersistenceUnit" transaction-type="JTA">
-        <exclude-unlisted-classes>false</exclude-unlisted-classes>
+        <exclude-unlisted-classes>true</exclude-unlisted-classes>
         <properties>
             <property name="javax.persistence.schema-generation.database.action" value="create"/>
         </properties>
     </persistence-unit>
 </persistence>

Prior to this change the following files were being generated:

  • target/generated-sources/annotations/com/greptilian/addressbookmvc/javaee7/Person_.java
  • target/classes/com/greptilian/addressbookmvc/javaee7/Person_.class

Additionally to the generation by ones IDE (as mentioned by Brian Vosburgh), the JPA Metamodel may be generated during the maven build, e.g. by the JPA Static Metamodel Generator. This happens, if the maven dependency

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>CURRENT-VERSION</version>
</dependency>

is used (e.g. listed in pom.xml).

The JPA Metamodel can be used with the JPA Criteria API, see also here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top