Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a reproducer for spring-projects/spring-framework#31050 #188

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -15,6 +15,9 @@ public interface AuthorRepository extends ListCrudRepository<Author, Long> {
@Query("SELECT a FROM Author a WHERE a.name = :name")
Optional<Author> queryFindByName(String name);

@Query(value = "SELECT * FROM author WHERE name = :name", nativeQuery = true)
Optional<Author> nativeQueryFindByName(String name);

Stream<Author> findByNameContaining(String name);

}
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 Expand Up @@ -51,7 +52,7 @@ private void deleteAll() {

private void queryFindByName() {
Author author1 = this.authorRepository.queryFindByName("Josh Long").orElse(null);
Author author2 = this.authorRepository.queryFindByName("Martin Kleppmann").orElse(null);
Author author2 = this.authorRepository.nativeQueryFindByName("Martin Kleppmann").orElse(null);

System.out.printf("queryFindByName(): author1 = %s%n", author1);
System.out.printf("queryFindByName(): author2 = %s%n", author2);
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.