Skip to content

Commit 64e9637

Browse files
mbelladebeikov
authored andcommitted
HHH-13627 Add test for issue
1 parent 0a2bf3f commit 64e9637

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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.test.cache;
8+
9+
import java.util.HashSet;
10+
import java.util.Objects;
11+
import java.util.Set;
12+
import javax.persistence.Cacheable;
13+
import javax.persistence.Column;
14+
import javax.persistence.Entity;
15+
import javax.persistence.FetchType;
16+
import javax.persistence.Id;
17+
import javax.persistence.JoinColumn;
18+
import javax.persistence.ManyToOne;
19+
import javax.persistence.OneToMany;
20+
21+
import org.hibernate.CacheMode;
22+
import org.hibernate.annotations.Cache;
23+
import org.hibernate.annotations.CacheConcurrencyStrategy;
24+
25+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* @author Marco Belladelli
34+
*/
35+
public class CacheModeGetUpdateTest extends BaseCoreFunctionalTestCase {
36+
private static final long PHONE_ID = 1L;
37+
private static final long PERSON_ID = 2L;
38+
39+
@Override
40+
protected Class<?>[] getAnnotatedClasses() {
41+
return new Class[] {
42+
Phone.class,
43+
Person.class
44+
};
45+
}
46+
47+
@Before
48+
public void setUp() {
49+
inTransaction( session -> {
50+
session.persist( new Phone( PHONE_ID, "123" ) );
51+
session.persist( new Person( PERSON_ID, "Marco" ) );
52+
} );
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
inTransaction( session -> {
58+
session.createQuery( "delete from Phone" ).executeUpdate();
59+
session.createQuery( "delete from Person" ).executeUpdate();
60+
} );
61+
}
62+
63+
@Test
64+
public void test() {
65+
inTransaction( session -> {
66+
session.setCacheMode( CacheMode.GET );
67+
final Phone phone = session.get( Phone.class, PHONE_ID );
68+
final Person person = session.get( Person.class, PERSON_ID );
69+
phone.setPerson( person );
70+
person.getPhones().add( phone );
71+
session.persist( phone );
72+
} );
73+
// in a different transaction
74+
inTransaction( session -> {
75+
final Phone phone = session.get( Phone.class, PHONE_ID );
76+
assertThat( phone.getPerson() ).isNotNull();
77+
} );
78+
}
79+
80+
@Entity( name = "Phone" )
81+
@Cacheable
82+
@Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
83+
public static class Phone {
84+
@Id
85+
private Long id;
86+
87+
@Column( name = "phone_number" )
88+
private String number;
89+
90+
@ManyToOne( fetch = FetchType.LAZY )
91+
@JoinColumn
92+
private Person person;
93+
94+
public Phone() {
95+
}
96+
97+
public Phone(final long id, final String number) {
98+
setId( id );
99+
setNumber( number );
100+
}
101+
102+
public Long getId() {
103+
return id;
104+
}
105+
106+
public void setId(Long id) {
107+
this.id = id;
108+
}
109+
110+
public String getNumber() {
111+
return number;
112+
}
113+
114+
public void setNumber(String number) {
115+
this.number = number;
116+
}
117+
118+
public Person getPerson() {
119+
return person;
120+
}
121+
122+
public void setPerson(Person person) {
123+
this.person = person;
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "Phone{" +
129+
"id=" + id +
130+
", number='" + number + '\'' +
131+
", person=" + person +
132+
'}';
133+
}
134+
135+
@Override
136+
public boolean equals(Object o) {
137+
if ( this == o ) {
138+
return true;
139+
}
140+
if ( o == null || getClass() != o.getClass() ) {
141+
return false;
142+
}
143+
Phone phone = (Phone) o;
144+
return Objects.equals( id, phone.id ) && Objects.equals( number, phone.number ) && Objects.equals(
145+
person,
146+
phone.person
147+
);
148+
}
149+
150+
@Override
151+
public int hashCode() {
152+
return Objects.hash( id, number, person );
153+
}
154+
}
155+
156+
@Entity( name = "Person" )
157+
@Cacheable
158+
@Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
159+
public static class Person {
160+
161+
@Id
162+
private Long id;
163+
164+
private String name;
165+
166+
@OneToMany( fetch = FetchType.LAZY, mappedBy = "person" )
167+
private final Set<Phone> phones = new HashSet<>();
168+
169+
public Person() {
170+
}
171+
172+
public Person(final long id, final String name) {
173+
setId( id );
174+
setName( name );
175+
}
176+
177+
public Long getId() {
178+
return id;
179+
}
180+
181+
public void setId(final Long id) {
182+
this.id = id;
183+
}
184+
185+
public String getName() {
186+
return name;
187+
}
188+
189+
@Override
190+
public String toString() {
191+
return "Person{" +
192+
"id=" + id +
193+
", name='" + name +
194+
'}';
195+
}
196+
197+
@Override
198+
public boolean equals(Object o) {
199+
if ( this == o ) {
200+
return true;
201+
}
202+
if ( o == null || getClass() != o.getClass() ) {
203+
return false;
204+
}
205+
Person person = (Person) o;
206+
return Objects.equals( id, person.id ) && Objects.equals( name, person.name );
207+
}
208+
209+
@Override
210+
public int hashCode() {
211+
return Objects.hash( id, name );
212+
}
213+
214+
public void setName(final String name) {
215+
this.name = name;
216+
}
217+
218+
public Set<Phone> getPhones() {
219+
return phones;
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)