简体   繁体   中英

Why is there a class with entity name plus an underscore for JPA entity class

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 ie 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;
}

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 "".

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, eg 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 (eg listed in pom.xml).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM