package-info.java中的@GenericGenerator不能正常工作JPA

时间:2017-02-18 09:14:36

标签: hibernate jpa annotations jpa-2.1 package-info

我尝试在Generator上添加package-info.java,以便我不必在每个@Entity课程上重写相同的代码。但是当我尝试运行程序应用程序时,我收到错误。

Session Factory could not be created.org.hibernate.AnnotationException: 
Unknown Id.generator: ID_GENERATOR

如果我在@GenericGenerator课程上应用@Entity,那就可以了。我在这里错过了一些关键点。

我发现了这个tread,但那里发布的答案对我没用。

配置

package-info.java

@org.hibernate.annotations.GenericGenerator(
    name = "ID_GENERATOR",
    strategy = "enhanced-sequence",
    parameters = {
        @org.hibernate.annotations.Parameter(
            name = "sequence_name",
            value = "JPA_SEQUENCE"
        ),
        @org.hibernate.annotations.Parameter(
            name = "initial_value",
            value = "1"
        )
    }
)
package com.example.hibernate.model;

Student.java

package com.example.hibernate.model;

@Entity
@Table(name = "student")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {

    @Id
    @GeneratedValue(generator = "ID_GENERATOR")
    private int id;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">

    <persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>


        <class>com.example.hibernate.model.Student</class>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:mem:test_mem" />
            <property name="hibernate.connection.user" value="sa" />
             <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
</persistence>

2 个答案:

答案 0 :(得分:1)

我找到的解决方案之一是在每个@javax.persistence.SequenceGenerator类上使用@Entity,例如

@Entity
@SequenceGenerator(name = "MY_S", sequenceName = "ID_GENERATOR")
public class Student implements Serializable {

    @Id
    @GeneratedValue(generator = "MY_S")
    private int id;

}

要比每个GenericGenerator类定义@Entity 复制和粘贴少一点。

我们也可以在每个@SequenceGenerator课程中消除@Entity的使用,但在这种情况下我们需要使用XML

创建orm.xml并在mapping-file上将此文件添加为persistence.xml

<mapping-file>META-INF/orm.xml</mapping-file>

orm.xml内,我们定义了sequence-generator

<sequence-generator name="MY_S"
                    sequence-name="ID_GENERATOR"/>

答案 1 :(得分:0)

您只需在persistence.xml中添加包含GenericGenerator的包。

示例

<class>com.example.hibernate.model</class>
<class>com.example.hibernate.model.Student</class>