【问题标题】:what is gradle missing to map hibernate?gradle映射休眠缺少什么?
【发布时间】:2015-08-31 20:43:40
【问题描述】:

我的 java 项目有单元测试。

我的代码使用休眠。

当我使用 junit 运行测试时 - 一切都通过了。

当我使用 gradle 运行测试时 - 出现映射错误:

Caused by: org.hibernate.MappingException: Unknown entity: linqmap.users.interfaces.model.UserRecord

和班级:

@Entity
@Table(name = "users")

public class UserRecord implements Serializable {

    private static final long serialVersionUID = 1L;
    public static short CLASS_VERSION = 3;

    @Transient
    public short objectVersion = CLASS_VERSION;

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id")
    public long ID;

    @Column(name = "user_name")
    public String userName;

    @Column(name = "email")
    public String email;

    @Column(name = "full_name") // will be deprecated in the future
    public String fullName;

    @Column(name = "password")
    public String password;

还有一个配置文件:

<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_1_0.xsd"
    version="1.0">

    <persistence-unit name="UsersDB">

        <!-- The provider only needs to be set if you use several JPA providers <provider>org.hibernate.ejb.HibernatePersistence</provider> -->

        <properties>
            <!-- Scan for annotated classes and Hibernate mapping XML files -->
            <property name="hibernate.archive.autodetection" value="class, hbm" />

            <!-- SQL stdout logging <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> 
                <property name="use_sql_comments" value="true"/> -->

            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="dbc:postgresql://localhost:9992/israel" />

gradle 中可能缺少什么?

【问题讨论】:

    标签: java hibernate gradle


    【解决方案1】:

    我认为问题不在于 gradle。这是因为 JPA 规范愚蠢地要求实体的类与 persistence.xml 文件位于相同的 jar/目录中。而且由于 Gradle 不会将“已编译”资源存储在与已编译类相同的输出目录中,因此 Hibernate 不会找到映射的实体。

    将此行添加到您的 gradle 构建中,它可能会很好

    sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
    

    【讨论】:

    • 冬眠者不知道这个问题吗?看起来像一个白痴错误...(顺便说一句,非常感谢您的解决方案!)
    • 从 Gradle 5.x 开始,您需要使用 sourceSets.main.java.outputDir 而不是 sourceSets.main.output.classesDir 请参阅:SourceSetOutput - Gradle DSL Version 5.5 docs.gradle.org/current/dsl/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    相关资源
    最近更新 更多