Elad Benda
Elad Benda

Reputation: 36654

what is gradle missing to map hibernate?

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?

Upvotes: 3

Views: 895

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

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

Upvotes: 3

Related Questions