diff --git a/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/InfiniteRecursionTest.java b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/InfiniteRecursionTest.java new file mode 100644 index 00000000..5c469f95 --- /dev/null +++ b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/InfiniteRecursionTest.java @@ -0,0 +1,45 @@ +package com.fasterxml.jackson.datatype.hibernate4; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.hibernate.Hibernate; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.hibernate4.data.Contrato; + +public class InfiniteRecursionTest extends BaseTest { + + // [Issue#70] + @Test + public void testInfinite() throws Exception { + + EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit"); + String expected = "{\"id\":1,\"numeroContrato\":\"100001-9\",\"parcelas\":[{\"id\":1,\"numeroParcela\":1}],\"liquidacoes\":[{\"id\":1,\"valorTotal\":10000,\"parcela\":{\"id\":1,\"numeroParcela\":1}}]}"; + + try { + EntityManager em = emf.createEntityManager(); + ObjectMapper mapper = mapperWithModule(true); + + final Contrato contrato1 = em.find(Contrato.class, 1L); + Hibernate.initialize(contrato1.getParcelas()); + Hibernate.initialize(contrato1.getLiquidacoes()); + assertEquals(expected, mapper.writer().writeValueAsString(contrato1)); + + em.clear(); + + final Contrato contrato2 = em.find(Contrato.class, 1L); + Hibernate.initialize(contrato2.getLiquidacoes()); + Hibernate.initialize(contrato2.getParcelas()); + assertEquals(expected, mapper.writer().writeValueAsString(contrato2)); + + + } finally { + emf.close(); + } + + } + +} diff --git a/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Contrato.java b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Contrato.java new file mode 100644 index 00000000..7b4ec4e6 --- /dev/null +++ b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Contrato.java @@ -0,0 +1,62 @@ +package com.fasterxml.jackson.datatype.hibernate4.data; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@Entity @Table(name="Contrato", catalog="classicmodels") +public class Contrato { + + @Id + private Long id; + + @Column(name="numero_contrato") + private String numeroContrato; + + @OneToMany(mappedBy="contrato") @JsonIgnoreProperties("contrato") + private List parcelas; + + @OneToMany(mappedBy="contrato") @JsonIgnoreProperties("contrato") + private List liquidacoes; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getNumeroContrato() { + return numeroContrato; + } + + public void setNumeroContrato(String numeroContrato) { + this.numeroContrato = numeroContrato; + } + + public List getParcelas() { + return parcelas; + } + + public void setParcelas(List parcelas) { + this.parcelas = parcelas; + } + + public List getLiquidacoes() { + return liquidacoes; + } + + public void setLiquidacoes(List liquidacoes) { + this.liquidacoes = liquidacoes; + } + + + +} diff --git a/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Liquidacao.java b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Liquidacao.java new file mode 100644 index 00000000..b4a8314d --- /dev/null +++ b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Liquidacao.java @@ -0,0 +1,64 @@ +package com.fasterxml.jackson.datatype.hibernate4.data; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@Entity @Table(name="Liquidacao", catalog="classicmodels") +public class Liquidacao { + + @Id + private Long id; + + @Column(name="valor_total") + private BigDecimal valorTotal; + + @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="contrato_id") + private Contrato contrato; + + @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="parcela_id") @JsonIgnoreProperties("contrato") + private Parcela parcela; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public BigDecimal getValorTotal() { + return valorTotal; + } + + public void setValorTotal(BigDecimal valorTotal) { + this.valorTotal = valorTotal; + } + + public Contrato getContrato() { + return contrato; + } + + public void setContrato(Contrato contrato) { + this.contrato = contrato; + } + + public Parcela getParcela() { + return parcela; + } + + public void setParcela(Parcela parcela) { + this.parcela = parcela; + } + + + +} \ No newline at end of file diff --git a/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Parcela.java b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Parcela.java new file mode 100644 index 00000000..aba7ece2 --- /dev/null +++ b/hibernate4/src/test/java/com/fasterxml/jackson/datatype/hibernate4/data/Parcela.java @@ -0,0 +1,50 @@ +package com.fasterxml.jackson.datatype.hibernate4.data; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity @Table(name="Parcela", catalog="classicmodels") +public class Parcela { + + @Id + private Long id; + + @Column(name="numero_parcela") + private Integer numeroParcela; + + @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="contrato_id") + private Contrato contrato; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getNumeroParcela() { + return numeroParcela; + } + + public void setNumeroParcela(Integer numeroParcela) { + this.numeroParcela = numeroParcela; + } + + public Contrato getContrato() { + return contrato; + } + + public void setContrato(Contrato contrato) { + this.contrato = contrato; + } + + + + +} diff --git a/hibernate4/src/test/resources/META-INF/persistence.xml b/hibernate4/src/test/resources/META-INF/persistence.xml index 9ff6f2cf..a624145b 100644 --- a/hibernate4/src/test/resources/META-INF/persistence.xml +++ b/hibernate4/src/test/resources/META-INF/persistence.xml @@ -6,12 +6,15 @@ org.hibernate.ejb.HibernatePersistence + com.fasterxml.jackson.datatype.hibernate4.data.Contrato com.fasterxml.jackson.datatype.hibernate4.data.Customer com.fasterxml.jackson.datatype.hibernate4.data.Employee + com.fasterxml.jackson.datatype.hibernate4.data.Liquidacao com.fasterxml.jackson.datatype.hibernate4.data.Office com.fasterxml.jackson.datatype.hibernate4.data.Order com.fasterxml.jackson.datatype.hibernate4.data.OrderDetail com.fasterxml.jackson.datatype.hibernate4.data.OrderDetailId + com.fasterxml.jackson.datatype.hibernate4.data.Parcela com.fasterxml.jackson.datatype.hibernate4.data.Payment com.fasterxml.jackson.datatype.hibernate4.data.PaymentId com.fasterxml.jackson.datatype.hibernate4.data.Product diff --git a/hibernate4/src/test/resources/classicmodels.sql b/hibernate4/src/test/resources/classicmodels.sql index fcaf9278..a84c2abf 100644 --- a/hibernate4/src/test/resources/classicmodels.sql +++ b/hibernate4/src/test/resources/classicmodels.sql @@ -3979,6 +3979,40 @@ INSERT INTO `classicmodels`.`Product` (`productCode`,`productName`,`productLine` ('S72_1253','Boeing X-32A JSF','Planes','1:72','Motor City Art Classics','10\" Wingspan with retractable landing gears.Comes with pilot',4857,32.77,49.66), ('S72_3212','Pont Yacht','Ships','1:72','Unimax Art Galleries','Measures 38 inches Long x 33 3/4 inches High. Includes a stand.\r\nMany extras including rigging, long boats, pilot house, anchors, etc. Comes with 2 masts, all square-rigged',414,33.3,54.6); + +DROP TABLE IF EXISTS `classicmodels`.`Contrato`; +CREATE TABLE `classicmodels`.`Contrato` ( + `id` numeric(10) NOT NULL, + `numero_contrato` varchar(50) NOT NULL, + PRIMARY KEY (`id`) +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +INSERT INTO `classicmodels`.`Contrato` (`id`,`numero_contrato`) VALUES + (1, '100001-9'); + +DROP TABLE IF EXISTS `classicmodels`.`Parcela`; +CREATE TABLE `classicmodels`.`Parcela` ( + `id` numeric(10) NOT NULL, + `numero_parcela` numeric(10) NOT NULL, + `contrato_id` numeric(10) NOT NULL, + PRIMARY KEY (`id`) +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +INSERT INTO `classicmodels`.`Parcela` (`id`,`numero_parcela`,`contrato_id`) VALUES + (1, 1, 1); + +DROP TABLE IF EXISTS `classicmodels`.`Liquidacao`; +CREATE TABLE `classicmodels`.`Liquidacao` ( + `id` numeric(10) NOT NULL, + `valor_total` numeric(10) NOT NULL, + `contrato_id` numeric(10) NOT NULL, + `parcela_id` numeric(10) NULL, + PRIMARY KEY (`id`) +) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +INSERT INTO `classicmodels`.`Liquidacao` (`id`,`valor_total`,`contrato_id`, `parcela_id`) VALUES + (1, 10000, 1, 1); + + + + -- Return to H2 regular mode SET MODE REGULAR;