Annotation mapping of Hibernate entities stopped working with the 1.0-milestone-4 release and is still a problem in 1.0-milestone-7


(kimcs) #1

Annotation mapping of Hibernate entities stopped working with the 1.0-milestone-4 release and is still a problem in 1.0-milestone-7.

The command “gradle clean test” will allow the single unit-test included below to pass when using the following gradle versions: 0.9.2, 1.0-milestone-1, 1.0-milestone-2, 1.0-milestone-3. The same unit-test and fail in all versions 1.0-milestone-4 and above.

I can work around the issue by using gradle 1.0-milestone-3, or by adding an xml file to configure the Hibernate entities rather than using annotations. I suspect this issues goes into the core of gradle, and is not really related to Hibernate, and that is why I chose to post the problem here first. I noticed there was another related forum post marked as “solved”, where the solution was described as using xml configuration, which I feel is really a workaround, and not a solution to whatever the underlying problem is.

Thank you for a great piece of software, I hope this report will help improve this great build system even further.

Here follows the complete file structure and file contents of a small hello-world hibernate project that demonstrate the issue.

/build.gradle

apply plugin: ‘java’

apply plugin: ‘eclipse’

repositories {

mavenCentral()

}

dependencies {

compile ‘org.hibernate:hibernate-entitymanager:3.6.1.Final’

compile ‘org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final’

compile (‘log4j:log4j:1.2.16’) {

exclude group: ‘com.sun.jdmk’, module: ‘jmxtools’

exclude group: ‘com.sun.jmx’, module: ‘jmxri’

exclude group: ‘javax.jms’,

module: ‘jms’

}

runtime ‘org.slf4j:slf4j-log4j12:1.6.1’

testCompile ‘junit:junit:4.8.1’

testRuntime ‘hsqldb:hsqldb:1.8.0.7’

}

/src/main/java/foo/Bar.java

package foo;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

@Entity

public class Bar {

@Id

@GeneratedValue

private int id;

private String greeting;

public Bar() {

}

public Bar(String greeting) {

this.greeting = greeting;

}

public int getId() {

return id;

}

public String getGreeting() {

return greeting;

}

}

/src/main/java/foo/BarDao.java

package foo;

import java.util.concurrent.Callable;

class BarDao {

public Bar findTheBar(final Database db) throws Exception {

return db.callInTransaction(new Callable() {

public Bar call() throws Exception {

return db.getEntityManager().find(Bar.class, 1);

}

});

}

}

/src/main/java/foo/Database.java

package foo;

import java.util.concurrent.Callable;

import javax.persistence.EntityManager;

import javax.persistence.EntityTransaction;

public class Database {

private final EntityManager entityManager;

public Database(EntityManager entityManager) {

this.entityManager = entityManager;

}

public EntityManager getEntityManager() {

return entityManager;

}

public T callInTransaction(Callable c) throws Exception {

EntityTransaction trans = entityManager.getTransaction();

trans.begin();

try {

T result = c.call();

trans.commit();

return result;

} finally {

if (trans.isActive()) {

trans.rollback();

}

}

}

}

src/main/resources/META-INF/persistence.xml

/src/test/java/foo/BarDaoTest.java

package foo;

import java.util.Properties;

import java.util.concurrent.Callable;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import org.apache.log4j.PropertyConfigurator;

import org.junit.Before;

import org.junit.Test;

public class BarDaoTest {

private Database db;

@Before

public void before() throws Exception {

// initialize log4j

Properties props = new Properties();

props.put(“log4j.appender.console”, “org.apache.log4j.ConsoleAppender”);

props.put(“log4j.appender.console.layout”, “org.apache.log4j.PatternLayout”);

props.put(“log4j.appender.console.layout.ConversionPattern”, “[%c %p] %m \n”);

props.put(“log4j.rootLogger”, “DEBUG, console”);

props.put(“log4j.logger.org.hibernate”, “INFO”);

PropertyConfigurator.configure(props);

// initialize persistence

EntityManagerFactory emf = Persistence.createEntityManagerFactory(“unittest”);

EntityManager em = emf.createEntityManager();

db = new Database(em);

// fill database with test data

db.callInTransaction(new Callable() {

public Object call() throws Exception {

Bar myEntity = new Bar(“Hello world from database!”);

db.getEntityManager().persist(myEntity);

return null;

}

});

}

@Test

public void testFindTheBar() throws Exception {

BarDao myEntityDao = new BarDao();

Bar helloEntity = myEntityDao.findTheBar(db);

System.out.println(helloEntity.getGreeting());

}

}


(kimcs) #2

I didn’t get the source for persistence.xml escaped property, here’s another try after consulting markdown dingus.

src/main/resources/META-INF/persistence.xml


(kimcs) #3

I didn’t get the source for persistence.xml escaped property, here’s another try after consulting markdown dingus.

src/main/resources/META-INF/persistence.xml

‘’


(kimcs) #4

I didn't get the source for persistence.xml escaped property, here's another try.

src/main/resources/META-INF/persistence.xml

``` <persistence version="1.0" 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"> <persistence-unit name="unittest"> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" /> <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:oc-test-db" /> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.connection.username" value="sa" /> <property name="hibernate.connection.password" value="" /> <property name="hibernate.connection.pool_size" value="7" /> <property name="hibernate.connection.isolation" value="1" /> <property name="hibernate.cache.use_second_level_cache" value="false" /> </properties> </persistence-unit> </persistence> ```


(kimcs) #5

Yet another attempt here to include the source of the xml file.

src/main/resources/META-INF/persistence.xml

<persistence version=“1.0” 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”> <persistence-unit name=“unittest”> <properties> <property name=“hibernate.dialect” value=“org.hibernate.dialect.HSQLDialect” /> <property name=“hibernate.connection.driver_class” value=“org.hsqldb.jdbcDriver” /> <property name=“hibernate.connection.url” value=“jdbc:hsqldb:mem:oc-test-db” /> <property name=“hibernate.hbm2ddl.auto” value=“create-drop” /> <property name=“hibernate.connection.username” value=“sa” /> <property name=“hibernate.connection.password” value="" /> <property name=“hibernate.connection.pool_size” value=“7” /> <property name=“hibernate.connection.isolation” value=“1” /> <property name=“hibernate.cache.use_second_level_cache” value=“false” /> </properties> </persistence-unit> </persistence>


(Peter Niederwieser) #6

Try:

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

See: http://forums.gradle.org/gradle/topics/regression_with_classloading_in_the_jetty_plugin_with_gradle_1_0_milestone_6


(kimcs) #7

That does indeed solve my problem. In my case, using a single folder for resource and classes output is just fine. Thank you very much!