|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.boot.models.xml.override; |
| 6 | + |
| 7 | +import jakarta.persistence.CollectionTable; |
| 8 | +import jakarta.persistence.Column; |
| 9 | +import jakarta.persistence.ElementCollection; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import jakarta.persistence.JoinColumn; |
| 13 | +import jakarta.persistence.ManyToOne; |
| 14 | +import jakarta.persistence.Table; |
| 15 | +import org.hibernate.annotations.OnDelete; |
| 16 | +import org.hibernate.annotations.OnDeleteAction; |
| 17 | +import org.hibernate.mapping.Collection; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.DomainModelScope; |
| 20 | +import org.hibernate.testing.orm.junit.Jira; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +/// The report (HHH-15510) states that `@JoinColumn` defined on the class |
| 28 | +/// is not processed when the class is "mapped" using XML - the class is |
| 29 | +/// really more just "specified" via XML. |
| 30 | +/// |
| 31 | +/// @author Steve Ebersole |
| 32 | +@DomainModel( |
| 33 | + xmlMappings = "org/hibernate/orm/test/jpa/xml/joincolumn-processing.xml" |
| 34 | +) |
| 35 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-15510" ) |
| 36 | +public class JoinColumnProcessingTests { |
| 37 | + @Test |
| 38 | + void verifyModel(DomainModelScope modelScope) { |
| 39 | + final var postBinding = modelScope.getEntityBinding( Post.class ); |
| 40 | + |
| 41 | + final var poster = postBinding.getProperty( "poster" ); |
| 42 | + final var posterValue = (org.hibernate.mapping.ManyToOne) poster.getValue(); |
| 43 | + assertThat( posterValue.getColumns() ).hasSize( 1 ); |
| 44 | + assertThat( posterValue.getColumns().get( 0 ).getName() ).isEqualTo( "poster_fk" ); |
| 45 | + |
| 46 | + final var tags = postBinding.getProperty( "tags" ); |
| 47 | + final var tagsValue = (Collection) tags.getValue(); |
| 48 | + assertThat( tagsValue.getCollectionTable().getName() ).isEqualTo( "post_tags" ); |
| 49 | + assertThat( tagsValue.getKey().getColumns() ).hasSize( 1 ); |
| 50 | + assertThat( tagsValue.getKey().getColumns().get( 0 ).getName() ).isEqualTo( "post_fk" ); |
| 51 | + assertThat( tagsValue.getKey().isCascadeDeleteEnabled() ).isTrue(); |
| 52 | + assertThat( tagsValue.getElement().getColumns() ).hasSize( 1 ); |
| 53 | + assertThat( tagsValue.getElement().getColumns().get( 0 ).getName() ).isEqualTo( "txt" ); |
| 54 | + } |
| 55 | + |
| 56 | + @Entity(name="Person") |
| 57 | + @Table(name="persons") |
| 58 | + public static class Person { |
| 59 | + @Id |
| 60 | + private Integer id; |
| 61 | + private String name; |
| 62 | + } |
| 63 | + |
| 64 | + @Entity(name="Post") |
| 65 | + @Table(name="posts") |
| 66 | + public static class Post { |
| 67 | + @Id |
| 68 | + private Integer id; |
| 69 | + private String name; |
| 70 | + @ManyToOne |
| 71 | + @JoinColumn(name="poster_fk") |
| 72 | + private Person poster; |
| 73 | + @ElementCollection |
| 74 | + @CollectionTable(name = "post_tags", |
| 75 | + joinColumns = @JoinColumn(name = "post_fk") |
| 76 | + ) |
| 77 | + @OnDelete(action = OnDeleteAction.CASCADE) |
| 78 | + @Column(name = "txt") |
| 79 | + private Set<String> tags; |
| 80 | + } |
| 81 | +} |
0 commit comments