|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.collections; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.hibernate.annotations.Cache; |
| 11 | +import org.hibernate.annotations.CacheConcurrencyStrategy; |
| 12 | +import org.hibernate.annotations.ListIndexBase; |
| 13 | +import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import jakarta.persistence.CascadeType; |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.FetchType; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import jakarta.persistence.JoinColumn; |
| 22 | +import jakarta.persistence.JoinTable; |
| 23 | +import jakarta.persistence.ManyToMany; |
| 24 | +import jakarta.persistence.ManyToOne; |
| 25 | +import jakarta.persistence.OneToMany; |
| 26 | +import jakarta.persistence.OrderColumn; |
| 27 | + |
| 28 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Selaron |
| 32 | + */ |
| 33 | +public class OrderColumnListIndexHHH18771ListInitializerTest extends BaseEntityManagerFunctionalTestCase { |
| 34 | + |
| 35 | + @Override |
| 36 | + protected Class<?>[] getAnnotatedClasses() { |
| 37 | + return new Class<?>[] { |
| 38 | + Person.class, |
| 39 | + Phone.class, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testLifecycle() { |
| 45 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 46 | + Person person = new Person( 1L ); |
| 47 | + entityManager.persist( person ); |
| 48 | + person.addPhone( new Phone( 1L ) ); |
| 49 | + person.getChildren().add( new Person( 2L ) ); |
| 50 | + person.getChildren().get( 0 ).setMother( person ); |
| 51 | + } ); |
| 52 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 53 | + entityManager.find( Person.class, 1L ); |
| 54 | + } ); |
| 55 | + } |
| 56 | + |
| 57 | + @Entity(name = "Person") |
| 58 | + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) |
| 59 | + public static class Person { |
| 60 | + |
| 61 | + @Id |
| 62 | + private Long id; |
| 63 | + |
| 64 | + @OneToMany(fetch = FetchType.EAGER, mappedBy = "person", cascade = CascadeType.ALL) |
| 65 | + @OrderColumn(name = "order_id") |
| 66 | + @ListIndexBase(100) |
| 67 | + private List<Phone> phones = new ArrayList<>(); |
| 68 | + |
| 69 | + @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) |
| 70 | + @JoinTable(name = "parent_child_relationships", joinColumns = @JoinColumn(name = "parent_id"), inverseJoinColumns = @JoinColumn(name = "child_id")) |
| 71 | + @OrderColumn(name = "pos") |
| 72 | + @ListIndexBase(200) |
| 73 | + private List<Person> children = new ArrayList<>(); |
| 74 | + |
| 75 | + @ManyToOne |
| 76 | + @JoinColumn(name = "mother_id") |
| 77 | + private Person mother; |
| 78 | + |
| 79 | + public Person() { |
| 80 | + } |
| 81 | + |
| 82 | + public Person(Long id) { |
| 83 | + this.id = id; |
| 84 | + } |
| 85 | + |
| 86 | + public List<Phone> getPhones() { |
| 87 | + return phones; |
| 88 | + } |
| 89 | + |
| 90 | + public void addPhone(Phone phone) { |
| 91 | + phones.add( phone ); |
| 92 | + phone.setPerson( this ); |
| 93 | + } |
| 94 | + |
| 95 | + public List<Person> getChildren() { |
| 96 | + return children; |
| 97 | + } |
| 98 | + |
| 99 | + public Person getMother() { |
| 100 | + return mother; |
| 101 | + } |
| 102 | + |
| 103 | + public void setMother(Person mother) { |
| 104 | + this.mother = mother; |
| 105 | + } |
| 106 | + |
| 107 | + public void removePhone(Phone phone) { |
| 108 | + phones.remove( phone ); |
| 109 | + phone.setPerson( null ); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Entity(name = "Phone") |
| 114 | + public static class Phone { |
| 115 | + |
| 116 | + @Id |
| 117 | + private Long id; |
| 118 | + |
| 119 | + @ManyToOne |
| 120 | + private Person person; |
| 121 | + |
| 122 | + public Phone() { |
| 123 | + } |
| 124 | + |
| 125 | + public Phone(Long id) { |
| 126 | + this.id = id; |
| 127 | + } |
| 128 | + |
| 129 | + public Long getId() { |
| 130 | + return id; |
| 131 | + } |
| 132 | + |
| 133 | + public Person getPerson() { |
| 134 | + return person; |
| 135 | + } |
| 136 | + |
| 137 | + public void setPerson(Person person) { |
| 138 | + this.person = person; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments