Skip to content

Commit 1b1552d

Browse files
committed
fix 'comment' and 'options' for @joincolumn
and add tests for JPA comments based on older tests for @comment
1 parent 4c381c5 commit 1b1552d

File tree

4 files changed

+194
-6
lines changed

4 files changed

+194
-6
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,12 @@ public class AnnotatedColumn {
9191
private String defaultValue;
9292
private String generatedAs;
9393

94-
// private String comment;
9594
private final List<CheckConstraint> checkConstraints = new ArrayList<>();
9695

9796
private AnnotatedColumns parent;
9897

99-
private String options;
100-
101-
private String comment;
98+
String options;
99+
String comment;
102100

103101
public AnnotatedColumns getParent() {
104102
return parent;
@@ -1050,4 +1048,7 @@ void setOptions(String options){
10501048
this.options = options;
10511049
}
10521050

1051+
void setComment(String comment){
1052+
this.comment = comment;
1053+
}
10531054
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumn.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public void applyJoinAnnotation(JoinColumn joinColumn, String defaultName) {
191191
setUpdatable( joinColumn.updatable() );
192192
setReferencedColumn( joinColumn.referencedColumnName() );
193193
applyColumnCheckConstraint( joinColumn );
194-
setOptions( joinColumn.options() );
194+
applyColumnComment( joinColumn );
195+
applyColumnOptions( joinColumn );
195196

196197
final String table = joinColumn.table();
197198
if ( table.isBlank() ) {
@@ -234,17 +235,20 @@ private static AnnotatedJoinColumn buildExplicitInheritanceJoinColumn(
234235
final String columnDefinition;
235236
final String referencedColumnName;
236237
final String options;
238+
final String comment;
237239
if ( primaryKeyJoinColumn != null ) {
238240
columnName = primaryKeyJoinColumn.name();
239241
columnDefinition = primaryKeyJoinColumn.columnDefinition();
240242
referencedColumnName = primaryKeyJoinColumn.referencedColumnName();
241243
options = primaryKeyJoinColumn.options();
244+
comment = null;
242245
}
243246
else {
244247
columnName = joinColumn.name();
245248
columnDefinition = joinColumn.columnDefinition();
246249
referencedColumnName = joinColumn.referencedColumnName();
247250
options = joinColumn.options();
251+
comment = joinColumn.comment();
248252
}
249253

250254
final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
@@ -259,6 +263,7 @@ private static AnnotatedJoinColumn buildExplicitInheritanceJoinColumn(
259263
// column.setPropertyHolder(propertyHolder);
260264
// column.setJoins(joins);
261265
// column.setContext( context );
266+
column.setComment( comment );
262267
column.setOptions( options );
263268
column.setImplicit( false );
264269
column.setNullable( false );
@@ -508,7 +513,7 @@ public AnnotatedJoinColumns getParent() {
508513

509514
@Override
510515
public void setParent(AnnotatedColumns parent) {
511-
if ( !(parent instanceof AnnotatedJoinColumns) ) {
516+
if ( !(parent instanceof AnnotatedJoinColumns) ) {
512517
throw new UnsupportedOperationException("wrong kind of parent");
513518
}
514519
super.setParent( parent );
@@ -521,4 +526,14 @@ public void setParent(AnnotatedJoinColumns parent) {
521526
private void applyColumnCheckConstraint(jakarta.persistence.JoinColumn column) {
522527
applyCheckConstraints( column.check() );
523528
}
529+
530+
private void applyColumnOptions(jakarta.persistence.JoinColumn column) {
531+
options = column.options();
532+
}
533+
534+
private void applyColumnComment(jakarta.persistence.JoinColumn column) {
535+
if ( !column.comment().isBlank() ) {
536+
comment = column.comment();
537+
}
538+
}
524539
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.annotations.comment.jpa;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.GeneratedValue;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.JoinColumn;
11+
import jakarta.persistence.ManyToOne;
12+
import org.hibernate.boot.Metadata;
13+
import org.hibernate.boot.MetadataSources;
14+
import org.hibernate.boot.registry.StandardServiceRegistry;
15+
import org.hibernate.mapping.Column;
16+
import org.hibernate.mapping.Table;
17+
import org.hibernate.testing.orm.junit.BaseUnitTest;
18+
import org.hibernate.testing.orm.junit.JiraKey;
19+
import org.hibernate.testing.util.ServiceRegistryUtil;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.stream.StreamSupport;
23+
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.core.Is.is;
26+
27+
/**
28+
* @author Yanming Zhou
29+
*/
30+
@BaseUnitTest
31+
public class CommentTest {
32+
33+
private static final String TABLE_NAME = "TestEntity";
34+
private static final String TABLE_COMMENT = "I am table";
35+
36+
@Test
37+
@JiraKey(value = "HHH-4369")
38+
public void testComments() {
39+
StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry();
40+
Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TestEntity.class).buildMetadata();
41+
Table table = StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false)
42+
.flatMap(namespace -> namespace.getTables().stream()).filter(t -> t.getName().equals(TABLE_NAME))
43+
.findFirst().orElse(null);
44+
assertThat(table.getComment(), is(TABLE_COMMENT));
45+
for (Column col : table.getColumns()) {
46+
assertThat(col.getComment(), is("I am " + col.getName()));
47+
}
48+
}
49+
50+
@Entity(name = "Person")
51+
@jakarta.persistence.Table(name = TABLE_NAME, comment = TABLE_COMMENT)
52+
public static class TestEntity {
53+
54+
@Id
55+
@GeneratedValue
56+
@jakarta.persistence.Column(comment = "I am id")
57+
private Long id;
58+
59+
@jakarta.persistence.Column(length = 50, comment = "I am name")
60+
private String name;
61+
62+
@ManyToOne
63+
@JoinColumn(name = "other", comment = "I am other")
64+
private TestEntity other;
65+
66+
}
67+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.annotations.comment.jpa;
6+
7+
import jakarta.persistence.AttributeOverride;
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Embeddable;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.JoinColumn;
14+
import jakarta.persistence.ManyToOne;
15+
import jakarta.persistence.SecondaryTable;
16+
import jakarta.persistence.Table;
17+
import org.hibernate.boot.Metadata;
18+
import org.hibernate.boot.MetadataSources;
19+
import org.hibernate.boot.registry.StandardServiceRegistry;
20+
import org.hibernate.testing.orm.junit.BaseUnitTest;
21+
import org.hibernate.testing.orm.junit.JiraKey;
22+
import org.hibernate.testing.util.ServiceRegistryUtil;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.math.BigDecimal;
26+
import java.time.LocalDate;
27+
import java.util.Currency;
28+
import java.util.stream.StreamSupport;
29+
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.hamcrest.core.Is.is;
32+
33+
/**
34+
* @author Yanming Zhou
35+
*/
36+
@BaseUnitTest
37+
public class CommentsTest {
38+
39+
private static final String TABLE_NAME = "TestEntity";
40+
private static final String SEC_TABLE_NAME = "TestEntity2";
41+
private static final String TABLE_COMMENT = "I am a table";
42+
private static final String SEC_TABLE_COMMENT = "I am a table too";
43+
44+
@Test
45+
@JiraKey(value = "HHH-4369")
46+
public void testComments() {
47+
StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry();
48+
Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(TestEntity.class).buildMetadata();
49+
org.hibernate.mapping.Table table = StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false)
50+
.flatMap(namespace -> namespace.getTables().stream()).filter(t -> t.getName().equals(TABLE_NAME))
51+
.findFirst().orElse(null);
52+
assertThat(table.getComment(), is(TABLE_COMMENT));
53+
assertThat(table.getColumns().size(), is(6));
54+
for (org.hibernate.mapping.Column col : table.getColumns()) {
55+
assertThat(col.getComment(), is("I am " + col.getName()));
56+
}
57+
table = StreamSupport.stream(metadata.getDatabase().getNamespaces().spliterator(), false)
58+
.flatMap(namespace -> namespace.getTables().stream()).filter(t -> t.getName().equals(SEC_TABLE_NAME))
59+
.findFirst().orElse(null);
60+
assertThat(table.getComment(), is(SEC_TABLE_COMMENT));
61+
assertThat(table.getColumns().size(), is(2));
62+
long count = table.getColumns().stream().filter(col -> "This is a date".equalsIgnoreCase(col.getComment())).count();
63+
assertThat(count, is(1L));
64+
}
65+
66+
@Entity(name = "Person")
67+
@Table(name = TABLE_NAME, comment = TABLE_COMMENT)
68+
@SecondaryTable(name = SEC_TABLE_NAME, comment = SEC_TABLE_COMMENT)
69+
public static class TestEntity {
70+
71+
@Id
72+
@GeneratedValue
73+
@Column(comment = "I am id")
74+
private Long id;
75+
76+
@AttributeOverride(name = "firstName", column = @Column(comment = "I am firstName"))
77+
@AttributeOverride(name = "lastName", column = @Column(comment = "I am lastName"))
78+
private Name name;
79+
80+
private Money money;
81+
82+
@Column(table = SEC_TABLE_NAME, comment = "This is a date")
83+
private LocalDate localDate;
84+
85+
@ManyToOne
86+
@JoinColumn(name = "other", comment = "I am other")
87+
private TestEntity other;
88+
89+
}
90+
91+
@Embeddable
92+
public static class Name {
93+
private String firstName;
94+
private String lastName;
95+
}
96+
97+
@Embeddable
98+
public static class Money {
99+
@Column(comment = "I am amount")
100+
private BigDecimal amount;
101+
@Column(comment = "I am currency")
102+
private Currency currency;
103+
}
104+
105+
}

0 commit comments

Comments
 (0)