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());
}
}