|
| 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.orm.test.records; |
| 8 | + |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import jakarta.persistence.Embeddable; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.IdClass; |
| 23 | +import jakarta.persistence.ManyToOne; |
| 24 | +import jakarta.persistence.MapsId; |
| 25 | +import jakarta.persistence.OneToMany; |
| 26 | +import jakarta.persistence.PrimaryKeyJoinColumn; |
| 27 | + |
| 28 | +import static jakarta.persistence.CascadeType.MERGE; |
| 29 | +import static jakarta.persistence.CascadeType.PERSIST; |
| 30 | +import static jakarta.persistence.CascadeType.REMOVE; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Marco Belladelli |
| 34 | + */ |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + RecordIdClassAndMapsIdTest.UserAuthorityEntity.class, |
| 37 | + RecordIdClassAndMapsIdTest.UserAuthorityId.class, |
| 38 | + RecordIdClassAndMapsIdTest.UserEntity.class, |
| 39 | +} ) |
| 40 | +@SessionFactory |
| 41 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18062" ) |
| 42 | +public class RecordIdClassAndMapsIdTest { |
| 43 | + @Test |
| 44 | + public void testMapping(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + final UserEntity ue = new UserEntity(); |
| 47 | + ue.setName( "user_1" ); |
| 48 | + final UserAuthorityEntity uae = new UserAuthorityEntity(); |
| 49 | + ue.addUserAuthority( uae ); |
| 50 | + uae.setUser( ue ); |
| 51 | + uae.setAuthority( "auth_1" ); |
| 52 | + session.persist( ue ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @Entity( name = "UserAuthorityEntity" ) |
| 57 | + @IdClass( UserAuthorityId.class ) |
| 58 | + static class UserAuthorityEntity { |
| 59 | + @Id |
| 60 | + private Long userId; |
| 61 | + |
| 62 | + @Id |
| 63 | + private String authority; |
| 64 | + |
| 65 | + @ManyToOne |
| 66 | + @MapsId( "userId" ) |
| 67 | + @PrimaryKeyJoinColumn( name = "user_id" ) |
| 68 | + private UserEntity user; |
| 69 | + |
| 70 | + public void setUser(UserEntity user) { |
| 71 | + this.user = user; |
| 72 | + } |
| 73 | + |
| 74 | + public void setAuthority(String authority) { |
| 75 | + this.authority = authority; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Embeddable |
| 80 | + record UserAuthorityId(Long userId, String authority) { |
| 81 | + } |
| 82 | + |
| 83 | + @Entity( name = "UserEntity" ) |
| 84 | + static class UserEntity { |
| 85 | + @Id |
| 86 | + @GeneratedValue |
| 87 | + private Long id; |
| 88 | + |
| 89 | + private String name; |
| 90 | + |
| 91 | + @OneToMany( cascade = { PERSIST, MERGE, REMOVE }, mappedBy = "user", orphanRemoval = true ) |
| 92 | + private Set<UserAuthorityEntity> userAuthorities = new HashSet<>(); |
| 93 | + |
| 94 | + public void setName(String name) { |
| 95 | + this.name = name; |
| 96 | + } |
| 97 | + |
| 98 | + public void addUserAuthority(UserAuthorityEntity userAuthority) { |
| 99 | + this.userAuthorities.add( userAuthority ); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments