Skip to content

Commit

Permalink
hibernate 5 module fix #439
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Aug 2, 2016
1 parent d6cf8e8 commit c77c137
Show file tree
Hide file tree
Showing 39 changed files with 3,579 additions and 11 deletions.
8 changes: 4 additions & 4 deletions coverage-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<configuration>
<sources>
<source>${project.parent.basedir}/jooby/src/main/java</source>
<source>${project.parent.basedir}/jooby-hbm4/src/main/java</source>
<source>${project.parent.basedir}/jooby-hbm/src/main/java</source>
<source>${project.parent.basedir}/jooby-hbs/src/main/java</source>
<source>${project.parent.basedir}/jooby-jackson/src/main/java</source>
<source>${project.parent.basedir}/jooby-jdbc/src/main/java</source>
Expand Down Expand Up @@ -88,7 +88,7 @@
<configuration>
<sources>
<source>${project.parent.basedir}/jooby/src/test/java</source>
<source>${project.parent.basedir}/jooby-hbm4/src/test/java</source>
<source>${project.parent.basedir}/jooby-hbm/src/test/java</source>
<source>${project.parent.basedir}/jooby-hbs/src/test/java</source>
<source>${project.parent.basedir}/jooby-jackson/src/test/java</source>
<source>${project.parent.basedir}/jooby-jdbc/src/test/java</source>
Expand Down Expand Up @@ -152,7 +152,7 @@
<directory>${project.parent.basedir}/jooby-hbs/src/test/resources</directory>
</resource>
<resource>
<directory>${project.parent.basedir}/jooby-hbm4/src/test/resources</directory>
<directory>${project.parent.basedir}/jooby-hbm/src/test/resources</directory>
</resource>
<resource>
<directory>${project.parent.basedir}/jooby-whoops/src/test/resources</directory>
Expand Down Expand Up @@ -278,7 +278,7 @@

<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-hbm4</artifactId>
<artifactId>jooby-hbm</artifactId>
<version>${project.version}</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class HbmCustomFeature extends ServerFeature {
use(ConfigFactory.empty()
.withValue("db.audit", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm("db.audit", Member.class));
use(new Hbm("db.audit").classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(Key.get(EntityManager.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jooby.hbm;

import java.net.URISyntaxException;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostLoadEvent;
import org.hibernate.event.spi.PostLoadEventListener;
import org.jooby.Results;
import org.jooby.hbm.data.Member;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class HbmEventListenerFeature extends ServerFeature {

public static class Service {
public String alias() {
return "post-load";
}
}

@SuppressWarnings("serial")
public static class MemberPostLoad implements PostLoadEventListener {

private Service service;

@Inject
public MemberPostLoad(final Service service) {
this.service = service;
}

@Override
public void onPostLoad(final PostLoadEvent event) {
Member member = (Member) event.getEntity();
member.setAlias(service.alias());
}

}

{
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm()
.onEvent(EventType.POST_LOAD, MemberPostLoad.class)
.classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(EntityManager.class);
TypedQuery<Member> query = em.createQuery("from Member", Member.class);
return query.getResultList().stream().map(m -> m.getAlias()).collect(Collectors.toList());
});

post("/members", (req, rsp, chain) -> {
Member member = req.params().to(Member.class);
EntityManager em = req.require(EntityManager.class);
em.persist(member);
if (req.param("err").toOptional(Boolean.class).orElse(false)) {
throw new IllegalArgumentException("Rollback on err");
}
// we do this way just to make sure the correct delegate got executed
rsp.send(Results.with(member));
chain.next(req, rsp);
});
}

@Test
public void hbm() throws URISyntaxException, Exception {
request()
.post("/members")
.form()
.add("id", 1)
.add("name", "pedro")
.expect("pedro(1)");

request()
.get("/members")
.expect("[post-load]");
}
}
4 changes: 3 additions & 1 deletion coverage-report/src/test/java/org/jooby/hbm/HbmFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class HbmFeature extends ServerFeature {
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm(Member.class));
use(new Hbm().classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(EntityManager.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jooby.hbm;

import java.net.URISyntaxException;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.jooby.Results;
import org.jooby.hbm.data.Member;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class HbmJpaListenerFeature extends ServerFeature {

{
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm()
.classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(EntityManager.class);
TypedQuery<Member> query = em.createQuery("from Member", Member.class);
return query.getResultList().stream().map(m -> m.getAlias()).collect(Collectors.toList());
});

post("/members", (req, rsp, chain) -> {
Member member = req.params().to(Member.class);
EntityManager em = req.require(EntityManager.class);
em.persist(member);
if (req.param("err").toOptional(Boolean.class).orElse(false)) {
throw new IllegalArgumentException("Rollback on err");
}
// we do this way just to make sure the correct delegate got executed
rsp.send(Results.with(member));
chain.next(req, rsp);
});
}

@Test
public void hbm() throws URISyntaxException, Exception {
request()
.post("/members")
.form()
.add("id", 1)
.add("name", "pedro")
.expect("pedro(1)");

request()
.get("/members")
.expect("[post-load]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class HbmParamConverterFeature extends ServerFeature {
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm(Member.class));
use(new Hbm().classes(Member.class));

use("*", Hbm.openSessionInView());

parser((type, ctx) -> {
if (type.getRawType() == Member.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class HbmProdEnvFeature extends ServerFeature {
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));

use(new Hbm(Member.class));
use(new Hbm().classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(EntityManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class HbmScanFeature extends ServerFeature {

use(new Hbm().scan());

use("*", Hbm.openSessionInView());

get("/members", req -> {
EntityManager em = req.require(EntityManager.class);
Query query = em.createQuery("from Member");
Expand Down
13 changes: 13 additions & 0 deletions coverage-report/src/test/java/org/jooby/hbm/data/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@
import java.util.Optional;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
@EntityListeners(MemberListener.class)
public class Member {

@Id
private int id;

private String name;

@Transient
private String alias;

@Transient
public Optional<Boolean> viewErr = Optional.empty();

public String getAlias() {
return alias;
}

public void setAlias(final String alias) {
this.alias = alias;
}

public int getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jooby.hbm.data;

import javax.inject.Inject;
import javax.persistence.PostLoad;

public class MemberListener {
public static class Service {
public String alias() {
return "post-load";
}
}

private Service service;

@Inject
public MemberListener(final Service service) {
this.service = service;
}

@PostLoad
public void onPostLoad(final Member member) {
member.setAlias(service.alias());
}
}
69 changes: 69 additions & 0 deletions coverage-report/src/test/java/org/jooby/issues/Issue439.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.jooby.issues;

import java.net.URISyntaxException;

import org.jooby.hbm.Hbm;
import org.jooby.hbm.UnitOfWork;
import org.jooby.hbm.data.Member;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class Issue439 extends ServerFeature {

{
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new Hbm().classes(Member.class));

use("*", Hbm.openSessionInView());

get("/members", req -> {
return req.require(UnitOfWork.class).apply(em -> {
return em.createQuery("from Member").getResultList();
});

});

post("/members", (req, rsp, chain) -> {
req.require(UnitOfWork.class).accept(em -> {
Member member = req.params().to(Member.class);
em.persist(member);
rsp.send(member);
chain.next(req, rsp);
});
});
}

@Test
public void hbm() throws URISyntaxException, Exception {
request()
.get("/members")
.expect("[]");

request()
.post("/members")
.form()
.add("id", 1)
.add("name", "pedro")
.expect("pedro(1)");

request()
.get("/members")
.expect("[pedro(1)]");

request()
.post("/members")
.form()
.add("id", 2)
.add("name", "pablo")
.expect("pablo(2)");

request()
.get("/members")
.expect("[pedro(1), pablo(2)]");
}
}
Loading

0 comments on commit c77c137

Please sign in to comment.