Skip to content

Commit

Permalink
add example for #441 (Assuming user exception is a RuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
mebigfatguy committed Nov 2, 2024
1 parent 3e00289 commit 20f5d65
Showing 1 changed file with 129 additions and 121 deletions.
250 changes: 129 additions & 121 deletions src/samples/java/ex/JPAI_Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubEntity> ss = new ArrayList<SubEntity>();
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<SubEntity> 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<SubEntity> getSubEntities() {
return subEntities;
}

public void setSubEntities(List<SubEntity> 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<SubEntity> ss = new ArrayList<SubEntity>();
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<SubEntity> 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<SubEntity> getSubEntities() {
return subEntities;
}

public void setSubEntities(List<SubEntity> 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 {

}
}

0 comments on commit 20f5d65

Please sign in to comment.