From 20f5d655fb83764f58cdb06bd0fbaf636492122b Mon Sep 17 00:00:00 2001 From: Dave Brosius Date: Sat, 2 Nov 2024 12:24:38 -0400 Subject: [PATCH] add example for #441 (Assuming user exception is a RuntimeException --- src/samples/java/ex/JPAI_Sample.java | 250 ++++++++++++++------------- 1 file changed, 129 insertions(+), 121 deletions(-) diff --git a/src/samples/java/ex/JPAI_Sample.java b/src/samples/java/ex/JPAI_Sample.java index 95af64a1b..852e61a89 100644 --- a/src/samples/java/ex/JPAI_Sample.java +++ b/src/samples/java/ex/JPAI_Sample.java @@ -23,126 +23,134 @@ public class JPAI_Sample { - EntityManager em; + EntityManager em; - @Transactional - private void writeData() { - } - - @Transactional - public void writeGoodData() { - } - - private void badWrite() { - writeGoodData(); - } - - public void ignoreMergeResult(MyEntity e, SubEntity s) { - - em.merge(e); - em.merge(s); - - List ss = new ArrayList(); - ss.add(s); - e.setSubEntities(ss); - - em.flush(); - } - - @Transactional - public void noRollbacks(MyEntity e) throws IOException { - - } - - @Transactional(rollbackFor = { SQLException.class, - CloneNotSupportedException.class }, noRollbackFor = IOException.class) - public void noDeclaredRollbackExceptions() { - } - - @Transactional(rollbackFor = SQLException.class, noRollbackFor = IOException.class) - public void fpDefinedRollBack(MyEntity e) throws SQLException, FileNotFoundException { - } - - @Transactional(readOnly = true) - public void fpReadOnlyExceptions(MyEntity e) throws IOException { - } - - @Entity - @Table(name = "MY_ENTITY") - public static class MyEntity { - - private Integer id; - private List subEntities; - - @Id - @SequenceGenerator(name = "MY_ENTITY_SEQ", sequenceName = "MY_ENTITY_SEQ", allocationSize = 100) - @GeneratedValue(generator = "MY_ENTITY_SEQ") - @Column(name = "ID", nullable = false, unique = true) - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "myEntity", targetEntity = MyEntity.class) - public List getSubEntities() { - return subEntities; - } - - public void setSubEntities(List subEntities) { - this.subEntities = subEntities; - } - - @Override - public int hashCode() { - return id.hashCode(); - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof MyEntity)) { - return false; - } - - MyEntity that = (MyEntity) o; - if (id == null) { - return that.id == null; - } - - return id.equals(that.id); - } - } - - @Entity - @Table(name = "SUB_ENTITY") - public static class SubEntity { - - public Integer subId; - public MyEntity myEntity; - - @Id - @SequenceGenerator(name = "SUB_ENTITY_SEQ", sequenceName = "SUB_ENTITY_SEQ", allocationSize = 100) - @GeneratedValue(generator = "SUB_ENTITY_SEQ") - @Column(name = "SUB_ID", nullable = false, unique = true) - public Integer getSubId() { - return subId; - } - - public void setSubId(Integer subId) { - this.subId = subId; - } - - @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false) - @ManyToOne(fetch = FetchType.EAGER, targetEntity = MyEntity.class) - public MyEntity getMyEntity() { - return myEntity; - } - - public void setMyEntity(MyEntity myEntity) { - this.myEntity = myEntity; - } - - } + @Transactional + private void writeData() { + } + + @Transactional + public void writeGoodData() { + } + + private void badWrite() { + writeGoodData(); + } + + public void ignoreMergeResult(MyEntity e, SubEntity s) { + + em.merge(e); + em.merge(s); + + List ss = new ArrayList(); + ss.add(s); + e.setSubEntities(ss); + + em.flush(); + } + + @Transactional + public void noRollbacks(MyEntity e) throws IOException { + + } + + @Transactional(rollbackFor = { SQLException.class, + CloneNotSupportedException.class }, noRollbackFor = IOException.class) + public void noDeclaredRollbackExceptions() { + } + + @Transactional(rollbackFor = SQLException.class, noRollbackFor = IOException.class) + public void fpDefinedRollBack(MyEntity e) throws SQLException, FileNotFoundException { + } + + @Transactional(readOnly = true) + public void fpReadOnlyExceptions(MyEntity e) throws IOException { + } + + @Transactional + public void customEx(MyEntity e) throws FP441Exception { + // FP441Exception is a runtime exception + } + + @Entity + @Table(name = "MY_ENTITY") + public static class MyEntity { + + private Integer id; + private List subEntities; + + @Id + @SequenceGenerator(name = "MY_ENTITY_SEQ", sequenceName = "MY_ENTITY_SEQ", allocationSize = 100) + @GeneratedValue(generator = "MY_ENTITY_SEQ") + @Column(name = "ID", nullable = false, unique = true) + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "myEntity", targetEntity = MyEntity.class) + public List getSubEntities() { + return subEntities; + } + + public void setSubEntities(List subEntities) { + this.subEntities = subEntities; + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof MyEntity)) { + return false; + } + + MyEntity that = (MyEntity) o; + if (id == null) { + return that.id == null; + } + + return id.equals(that.id); + } + } + + @Entity + @Table(name = "SUB_ENTITY") + public static class SubEntity { + + public Integer subId; + public MyEntity myEntity; + + @Id + @SequenceGenerator(name = "SUB_ENTITY_SEQ", sequenceName = "SUB_ENTITY_SEQ", allocationSize = 100) + @GeneratedValue(generator = "SUB_ENTITY_SEQ") + @Column(name = "SUB_ID", nullable = false, unique = true) + public Integer getSubId() { + return subId; + } + + public void setSubId(Integer subId) { + this.subId = subId; + } + + @JoinColumn(name = "ID", referencedColumnName = "ID", nullable = false) + @ManyToOne(fetch = FetchType.EAGER, targetEntity = MyEntity.class) + public MyEntity getMyEntity() { + return myEntity; + } + + public void setMyEntity(MyEntity myEntity) { + this.myEntity = myEntity; + } + } + + public class FP441Exception extends RuntimeException { + + } }