Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
The related error happen with Hibernate 6.2 with
custom find methods on non-transactional use cases.

See gh-188
  • Loading branch information
sdeleuze authored and wilkinsona committed Sep 21, 2023
1 parent ed4fe18 commit 56263e7
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,16 @@ void abstractPersistable(AssertableOutput output) {
});
}

@Test
void listVouchers(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining(
"listVouchers(): voucher = Voucher{id=1, msisdn='0810000000', status=0, dateCreated")
.hasSingleLineContaining(
"listVouchers(): voucher = Voucher{id=2, msisdn='0810000000', status=1, dateCreated=");

});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.data.jpa;

import java.util.List;

import com.example.data.jpa.model.Voucher;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class NonTransactionalRunner implements CommandLineRunner {

private final VoucherRepository voucherRepository;

public NonTransactionalRunner(VoucherRepository voucherRepository) {
this.voucherRepository = voucherRepository;
}

@Override
public void run(String... args) {
insertVouchers();
listVouchers();
}

private void insertVouchers() {
this.voucherRepository.save(new Voucher("0810000000", 0));
this.voucherRepository.save(new Voucher("0810000000", 1));
}

private void listVouchers() {
List<Voucher> vouchers = this.voucherRepository.findByMsisdn("0810000000");
vouchers.forEach(voucher -> System.out.printf("listVouchers(): voucher = %s%n", voucher));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
import org.springframework.transaction.annotation.Transactional;

@Component
class CLR implements CommandLineRunner {
class TransactionalRunner implements CommandLineRunner {

private final AuthorRepository authorRepository;

private final BookRepository bookRepository;

private final PublisherRepository publisherRepository;

CLR(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) {
TransactionalRunner(AuthorRepository authorRepository, BookRepository bookRepository,
PublisherRepository publisherRepository) {
this.authorRepository = authorRepository;
this.bookRepository = bookRepository;
this.publisherRepository = publisherRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.data.jpa;

import java.util.List;

import com.example.data.jpa.model.Voucher;

import org.springframework.data.repository.CrudRepository;

public interface VoucherRepository extends CrudRepository<Voucher, Integer> {

List<Voucher> findByMsisdn(String msisdn);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.example.data.jpa.model;

import java.util.Date;
import java.util.Objects;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

@Entity
public class Voucher {

@Id
@GeneratedValue
private Integer id;

private String msisdn;

private int status;

private Date dateCreated;

protected Voucher() {
}

public Voucher(String msisdn, int status) {
this.id = null;
this.msisdn = msisdn;
this.status = status;
this.dateCreated = new Date();
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMsisdn() {
return msisdn;
}

public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public Date getDateCreated() {
return dateCreated;
}

public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Voucher voucher = (Voucher) o;
return status == voucher.status && Objects.equals(id, voucher.id) && Objects.equals(msisdn, voucher.msisdn)
&& Objects.equals(dateCreated, voucher.dateCreated);
}

@Override
public int hashCode() {
return Objects.hash(id, msisdn, status, dateCreated);
}

@Override
public String toString() {
return "Voucher{" + "id=" + id + ", msisdn='" + msisdn + '\'' + ", status=" + status + ", dateCreated="
+ dateCreated + '}';
}

}
12 changes: 0 additions & 12 deletions data/data-jpa/src/main/resources/schema.sql

This file was deleted.

0 comments on commit 56263e7

Please sign in to comment.