|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.mapping.inheritance.version; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.Inheritance; |
| 24 | +import jakarta.persistence.InheritanceType; |
| 25 | +import jakarta.persistence.OneToMany; |
| 26 | +import jakarta.persistence.PrimaryKeyJoinColumn; |
| 27 | +import jakarta.persistence.Version; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Marco Belladelli |
| 33 | + */ |
| 34 | +@SessionFactory |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + InheritanceVersionedParentTest.VersionedFruit.class, |
| 37 | + InheritanceVersionedParentTest.Raspberry.class, |
| 38 | + InheritanceVersionedParentTest.Drupelet.class |
| 39 | +} ) |
| 40 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16667" ) |
| 41 | +public class InheritanceVersionedParentTest { |
| 42 | + @BeforeAll |
| 43 | + public void setUp(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + final Drupelet drupelet = new Drupelet(); |
| 46 | + session.persist( drupelet ); |
| 47 | + final Raspberry raspberry = new Raspberry( 1L, "green" ); |
| 48 | + raspberry.getDrupelets().add( drupelet ); |
| 49 | + session.persist( raspberry ); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterAll |
| 54 | + public void tearDown(SessionFactoryScope scope) { |
| 55 | + scope.inTransaction( session -> session.createQuery( "from Raspberry", Raspberry.class ) |
| 56 | + .getResultList() |
| 57 | + .forEach( r -> { |
| 58 | + r.getDrupelets().clear(); |
| 59 | + session.remove( r ); |
| 60 | + } ) ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testUpdateBasic(SessionFactoryScope scope) { |
| 65 | + final Long prev = scope.fromTransaction( session -> { |
| 66 | + final Raspberry raspberry = session.find( Raspberry.class, 1L ); |
| 67 | + raspberry.setMaturity( "ripe" ); |
| 68 | + return raspberry.getVersion(); |
| 69 | + } ); |
| 70 | + scope.inTransaction( |
| 71 | + session -> assertThat( session.find( Raspberry.class, 1L ).getVersion() ).isEqualTo( prev + 1 ) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testUpdateAssociation(SessionFactoryScope scope) { |
| 77 | + final Long prev = scope.fromTransaction( session -> { |
| 78 | + final Raspberry raspberry = session.find( Raspberry.class, 1L ); |
| 79 | + raspberry.getDrupelets().clear(); |
| 80 | + return raspberry.getVersion(); |
| 81 | + } ); |
| 82 | + scope.inTransaction( |
| 83 | + session -> assertThat( session.find( Raspberry.class, 1L ).getVersion() ).isEqualTo( prev + 1 ) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + @Entity( name = "VersionedFruit" ) |
| 88 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 89 | + public static abstract class VersionedFruit { |
| 90 | + @Id |
| 91 | + private Long id; |
| 92 | + |
| 93 | + @Version |
| 94 | + private Long version; |
| 95 | + |
| 96 | + public VersionedFruit() { |
| 97 | + } |
| 98 | + |
| 99 | + public VersionedFruit(Long id) { |
| 100 | + this.id = id; |
| 101 | + } |
| 102 | + |
| 103 | + public Long getId() { |
| 104 | + return id; |
| 105 | + } |
| 106 | + |
| 107 | + public Long getVersion() { |
| 108 | + return version; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Entity( name = "Raspberry" ) |
| 113 | + @PrimaryKeyJoinColumn( name = "raspberry_id" ) |
| 114 | + public static class Raspberry extends VersionedFruit { |
| 115 | + @OneToMany |
| 116 | + private Set<Drupelet> drupelets = new HashSet<>(); |
| 117 | + |
| 118 | + private String maturity; |
| 119 | + |
| 120 | + public Raspberry() { |
| 121 | + } |
| 122 | + |
| 123 | + public Raspberry(Long id, String maturity) { |
| 124 | + super( id ); |
| 125 | + this.maturity = maturity; |
| 126 | + } |
| 127 | + |
| 128 | + public Set<Drupelet> getDrupelets() { |
| 129 | + return drupelets; |
| 130 | + } |
| 131 | + |
| 132 | + public String getMaturity() { |
| 133 | + return maturity; |
| 134 | + } |
| 135 | + |
| 136 | + public void setMaturity(String name) { |
| 137 | + this.maturity = name; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Entity( name = "Drupelet" ) |
| 142 | + public static class Drupelet { |
| 143 | + @Id |
| 144 | + @GeneratedValue |
| 145 | + private Long id; |
| 146 | + |
| 147 | + public Long getId() { |
| 148 | + return id; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments