什么是gradle缺少映射休眠? [英] what is gradle missing to map hibernate?

查看:125
本文介绍了什么是gradle缺少映射休眠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码使用hibernate。



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



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

 造成者:org.hibernate.MappingException:未知实体:linqmap.users.interfaces.model.UserRecord 

和类:

  @Entity 
@Table(name =用户)

public class UserRecord实现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)
公共长ID;

@Column(name =user_name)
public String userName;

@Column(name =email)
public String email;

@Column(name =full_name)//将来会被弃用
public String fullName;

@Column(name =password)
public String password;

和一个配置文件:

 < persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns: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>

<! - 如果您使用多个JPA提供程序< provider> org.hibernate.ejb.HibernatePersistence< / provider>,则只需设置提供程序。 - >

<属性>
<! - 扫描注释类和Hibernate映射XML文件 - >
< property name =hibernate.archive.autodetectionvalue =class,hbm/>

<! - SQL stdout logging< property name =hibernate.show_sqlvalue =true/> < property name =hibernate.format_sqlvalue =true/>
< property name =use_sql_commentsvalue =true/> - >

< property name =hibernate.connection.driver_classvalue =org.postgresql.Driver/>
< property name =hibernate.connection.urlvalue =dbc:postgresql:// localhost:9992 / israel/>

gradle中可以丢失什么?

解决方案

我认为这个问题不是真的与gradle。事实上,JPA规范愚蠢地要求实体的类与persistence.xml文件在同一个jar /目录中。由于Gradle并未将编译的资源与编译的类存储在相同的输出目录中,因此Hibernate没有找到映射的实体。



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

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


I have unittest to my java project.

My code uses hibernate.

When i run the test using junit - everything passes.

When I run the test using gradle - I get a mapping error:

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

and the class:

@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;

and a config file:

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

what can be missing in gradle?

解决方案

I think the problem is not really with gradle. It's with the fact that the JPA spec stupidly requires that the classes of the entities are in the same jar/directory as the persistence.xml file. And since Gradle doesn't store the "compiled" resources in the same output directory as the compiled classes, Hibernate doesn't find the mapped entities.

Add this line to your gradle build, and it will probably be fine

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

这篇关于什么是gradle缺少映射休眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆