Skip to content

Commit 32882dd

Browse files
committed
HHH-18756 Add test for issue
1 parent 50a9102 commit 32882dd

6 files changed

+976
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.embeddable.generated;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.CurrentTimestamp;
12+
import org.hibernate.annotations.SourceType;
13+
import org.hibernate.generator.EventType;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.util.Date;
21+
22+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23+
24+
@DomainModel(
25+
annotatedClasses = {
26+
CurrentTimeStampInsertEventTimeTest.TestEntity.class
27+
}
28+
)
29+
@SessionFactory
30+
@JiraKey("HHH-18756")
31+
public class CurrentTimeStampInsertEventTimeTest {
32+
33+
@Test
34+
public void testCurrentTimeStamp(SessionFactoryScope scope) {
35+
Long testEntityId = 1L;
36+
String name = "a";
37+
String value = "b";
38+
Date now = new Date();
39+
40+
scope.inTransaction(
41+
session -> {
42+
TestEntity testEntity = new TestEntity( testEntityId, name, value );
43+
testEntity.anEmbeddable.setTimestamp( now );
44+
session.persist( testEntity );
45+
}
46+
);
47+
48+
scope.inTransaction(
49+
session -> {
50+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
51+
assertThat( testEntity ).isNotNull();
52+
assertThat( testEntity.getName() ).isEqualTo( name );
53+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
54+
assertThat( anEmbeddable ).isNotNull();
55+
assertThat( anEmbeddable.getaString() ).isEqualTo( value );
56+
assertThat( anEmbeddable.getTimestamp() ).isNull();
57+
anEmbeddable.setTimestamp( now );
58+
}
59+
);
60+
61+
scope.inTransaction( session -> {
62+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
63+
assertThat( testEntity ).isNotNull();
64+
assertThat( testEntity.getName() ).isEqualTo( name );
65+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
66+
assertThat( anEmbeddable ).isNotNull();
67+
assertThat( anEmbeddable.getaString() ).isEqualTo( value );
68+
assertThat( anEmbeddable.getTimestamp() ).isNotNull();
69+
} );
70+
}
71+
72+
@Entity(name = "TestEntity")
73+
public static class TestEntity {
74+
@Id
75+
private Long id;
76+
77+
private String name;
78+
79+
@Embedded
80+
private AnEmbeddable anEmbeddable;
81+
82+
public TestEntity() {
83+
}
84+
85+
public TestEntity(Long id, String name, String aString) {
86+
this.id = id;
87+
this.name = name;
88+
anEmbeddable = new AnEmbeddable( aString );
89+
}
90+
91+
public Long getId() {
92+
return id;
93+
}
94+
95+
public String getName() {
96+
return name;
97+
}
98+
99+
public AnEmbeddable getAnEmbeddable() {
100+
return anEmbeddable;
101+
}
102+
}
103+
104+
public static class AnEmbeddable {
105+
@CurrentTimestamp(event = EventType.INSERT, source = SourceType.DB)
106+
@Column(name = "timestamp_column")
107+
private Date timestamp;
108+
109+
private String aString;
110+
111+
public AnEmbeddable() {
112+
}
113+
114+
public AnEmbeddable(String aString) {
115+
this.aString = aString;
116+
}
117+
118+
public Date getTimestamp() {
119+
return timestamp;
120+
}
121+
122+
public void setTimestamp(Date timestamp) {
123+
this.timestamp = timestamp;
124+
}
125+
126+
public String getaString() {
127+
return aString;
128+
}
129+
}
130+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.embeddable.generated;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.CurrentTimestamp;
12+
import org.hibernate.annotations.SourceType;
13+
import org.hibernate.testing.orm.junit.DomainModel;
14+
import org.hibernate.testing.orm.junit.JiraKey;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.Date;
20+
21+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
22+
23+
@DomainModel(
24+
annotatedClasses = {
25+
CurrentTimeStampInsertUpdateEventTimeTest.TestEntity.class
26+
}
27+
)
28+
@SessionFactory
29+
@JiraKey("HHH-18756")
30+
public class CurrentTimeStampInsertUpdateEventTimeTest {
31+
32+
@Test
33+
public void testCurrentTimeStamp(SessionFactoryScope scope) {
34+
Long testEntityId = 1L;
35+
String name = "a";
36+
String value = "b";
37+
String updatedValue = "b1";
38+
scope.inTransaction(
39+
session -> {
40+
TestEntity testEntity = new TestEntity( testEntityId, name, value );
41+
session.persist( testEntity );
42+
}
43+
);
44+
45+
Date now = new Date();
46+
47+
scope.inTransaction(
48+
session -> {
49+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
50+
assertThat( testEntity ).isNotNull();
51+
assertThat( testEntity.getName() ).isEqualTo( name );
52+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
53+
assertThat( anEmbeddable ).isNotNull();
54+
assertThat( anEmbeddable.getaString() ).isEqualTo( value );
55+
assertThat( anEmbeddable.getTimestamp() ).isNull();
56+
anEmbeddable.setTimestamp( now );
57+
anEmbeddable.setaString( updatedValue );
58+
}
59+
);
60+
61+
scope.inTransaction( session -> {
62+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
63+
assertThat( testEntity ).isNotNull();
64+
assertThat( testEntity.getName() ).isEqualTo( name );
65+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
66+
assertThat( anEmbeddable ).isNotNull();
67+
assertThat( anEmbeddable.getaString() ).isEqualTo( updatedValue );
68+
assertThat( anEmbeddable.getTimestamp() ).isNull();
69+
} );
70+
}
71+
72+
@Entity(name = "TestEntity")
73+
public static class TestEntity {
74+
@Id
75+
private Long id;
76+
77+
private String name;
78+
79+
@Embedded
80+
private AnEmbeddable anEmbeddable;
81+
82+
public TestEntity() {
83+
}
84+
85+
public TestEntity(Long id, String name, String aString) {
86+
this.id = id;
87+
this.name = name;
88+
anEmbeddable = new AnEmbeddable( aString );
89+
}
90+
91+
public Long getId() {
92+
return id;
93+
}
94+
95+
public String getName() {
96+
return name;
97+
}
98+
99+
public AnEmbeddable getAnEmbeddable() {
100+
return anEmbeddable;
101+
}
102+
}
103+
104+
public static class AnEmbeddable {
105+
@CurrentTimestamp(source = SourceType.DB)
106+
@Column(name = "timestamp_column")
107+
private Date timestamp;
108+
109+
private String aString;
110+
111+
public AnEmbeddable() {
112+
}
113+
114+
public AnEmbeddable(String aString) {
115+
this.aString = aString;
116+
}
117+
118+
public Date getTimestamp() {
119+
return timestamp;
120+
}
121+
122+
public void setTimestamp(Date timestamp) {
123+
this.timestamp = timestamp;
124+
}
125+
126+
public String getaString() {
127+
return aString;
128+
}
129+
130+
public void setaString(String aString) {
131+
this.aString = aString;
132+
}
133+
}
134+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.embeddable.generated;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embedded;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import org.hibernate.annotations.CurrentTimestamp;
12+
import org.hibernate.annotations.SourceType;
13+
import org.hibernate.generator.EventType;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.util.Date;
21+
22+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23+
24+
@DomainModel(
25+
annotatedClasses = {
26+
CurrentTimeStampUpdateEventTimeTest.TestEntity.class
27+
}
28+
)
29+
@SessionFactory
30+
@JiraKey("HHH-18756")
31+
public class CurrentTimeStampUpdateEventTimeTest {
32+
33+
@Test
34+
public void testCurrentTimeStamp(SessionFactoryScope scope) {
35+
Long testEntityId = 1L;
36+
String name = "a";
37+
String value = "b";
38+
Date now = new Date();
39+
40+
scope.inTransaction(
41+
session -> {
42+
TestEntity testEntity = new TestEntity( testEntityId, name, value );
43+
testEntity.getAnEmbeddable().setTimestamp( now );
44+
session.persist( testEntity );
45+
}
46+
);
47+
48+
scope.inTransaction(
49+
session -> {
50+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
51+
assertThat( testEntity ).isNotNull();
52+
assertThat( testEntity.getName() ).isEqualTo( name );
53+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
54+
assertThat( anEmbeddable ).isNotNull();
55+
assertThat( anEmbeddable.getaString() ).isEqualTo( value );
56+
// event time is UPDATE so it should be inserted
57+
assertThat( anEmbeddable.getTimestamp() ).isNotNull();
58+
// but not updated
59+
anEmbeddable.setTimestamp( null );
60+
}
61+
);
62+
63+
scope.inTransaction( session -> {
64+
TestEntity testEntity = session.find( TestEntity.class, testEntityId );
65+
assertThat( testEntity ).isNotNull();
66+
assertThat( testEntity.getName() ).isEqualTo( name );
67+
AnEmbeddable anEmbeddable = testEntity.getAnEmbeddable();
68+
assertThat( anEmbeddable ).isNotNull();
69+
assertThat( anEmbeddable.getaString() ).isEqualTo( value );
70+
assertThat( anEmbeddable.getTimestamp() ).isNotNull();
71+
} );
72+
}
73+
74+
@Entity(name = "TestEntity")
75+
public static class TestEntity {
76+
@Id
77+
private Long id;
78+
79+
private String name;
80+
81+
@Embedded
82+
private AnEmbeddable anEmbeddable;
83+
84+
public TestEntity() {
85+
}
86+
87+
public TestEntity(Long id, String name, String aString) {
88+
this.id = id;
89+
this.name = name;
90+
anEmbeddable = new AnEmbeddable( aString );
91+
}
92+
93+
public Long getId() {
94+
return id;
95+
}
96+
97+
public String getName() {
98+
return name;
99+
}
100+
101+
public AnEmbeddable getAnEmbeddable() {
102+
return anEmbeddable;
103+
}
104+
}
105+
106+
public static class AnEmbeddable {
107+
@CurrentTimestamp(event = EventType.UPDATE, source = SourceType.DB)
108+
@Column(name = "timestamp_column")
109+
private Date timestamp;
110+
111+
private String aString;
112+
113+
public AnEmbeddable() {
114+
}
115+
116+
public AnEmbeddable(String aString) {
117+
this.aString = aString;
118+
}
119+
120+
public Date getTimestamp() {
121+
return timestamp;
122+
}
123+
124+
public void setTimestamp(Date timestamp) {
125+
this.timestamp = timestamp;
126+
}
127+
128+
public String getaString() {
129+
return aString;
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)