|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.processor.test.embeddable; |
| 6 | + |
| 7 | +import jakarta.persistence.Access; |
| 8 | +import jakarta.persistence.AccessType; |
| 9 | +import jakarta.persistence.Embeddable; |
| 10 | +import jakarta.persistence.Embedded; |
| 11 | +import jakarta.persistence.Entity; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.MappedSuperclass; |
| 14 | +import jakarta.persistence.Transient; |
| 15 | +import org.hibernate.processor.test.util.CompilationTest; |
| 16 | +import org.hibernate.processor.test.util.TestUtil; |
| 17 | +import org.hibernate.processor.test.util.WithClasses; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import java.time.Instant; |
| 21 | +import java.time.LocalDateTime; |
| 22 | +import java.time.OffsetDateTime; |
| 23 | +import java.time.ZoneOffset; |
| 24 | + |
| 25 | +import static org.hibernate.processor.test.util.TestUtil.assertAttributeTypeInMetaModelFor; |
| 26 | +import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor; |
| 27 | + |
| 28 | +public class ForcedAccessTypeTest extends CompilationTest { |
| 29 | + @Test |
| 30 | + @WithClasses({MyEntity.class, DateAndTime.class, MySuperclass.class}) |
| 31 | + public void testCorrectAccessTypeUsedForEmbeddable() { |
| 32 | + System.out.println( TestUtil.getMetaModelSourceAsString( MyEntity.class ) ); |
| 33 | + assertMetamodelClassGeneratedFor( MyEntity.class ); |
| 34 | + |
| 35 | + assertMetamodelClassGeneratedFor( MySuperclass.class ); |
| 36 | + assertAttributeTypeInMetaModelFor( |
| 37 | + MySuperclass.class, |
| 38 | + "name", |
| 39 | + String.class, |
| 40 | + "Missing attribute name of type java.lang.String" |
| 41 | + ); |
| 42 | + |
| 43 | + assertMetamodelClassGeneratedFor( DateAndTime.class ); |
| 44 | + assertAttributeTypeInMetaModelFor( |
| 45 | + DateAndTime.class, |
| 46 | + "localDateTime", |
| 47 | + LocalDateTime.class, |
| 48 | + "Missing attribute localDateTime of type java.time.LocalDateTime" |
| 49 | + ); |
| 50 | + assertAttributeTypeInMetaModelFor( |
| 51 | + DateAndTime.class, |
| 52 | + "offset", |
| 53 | + ZoneOffset.class, |
| 54 | + "Missing attribute offset of type java.time.ZoneOffset" |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Entity |
| 59 | + @Access(AccessType.FIELD) |
| 60 | + static class MyEntity extends MySuperclass { |
| 61 | + @Id |
| 62 | + Long id; |
| 63 | + |
| 64 | + @Embedded |
| 65 | + DateAndTime dateAndTime; |
| 66 | + } |
| 67 | + |
| 68 | + @MappedSuperclass |
| 69 | + @Access(AccessType.PROPERTY) |
| 70 | + static class MySuperclass { |
| 71 | + private String name; |
| 72 | + |
| 73 | + public String getName() { |
| 74 | + return name; |
| 75 | + } |
| 76 | + |
| 77 | + public void setName(String name) { |
| 78 | + this.name = name; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Embeddable |
| 83 | + @Access(AccessType.PROPERTY) |
| 84 | + static class DateAndTime { |
| 85 | + |
| 86 | + @Transient |
| 87 | + private OffsetDateTime dateTime; |
| 88 | + |
| 89 | + public DateAndTime() { |
| 90 | + this.dateTime = OffsetDateTime.now(); |
| 91 | + } |
| 92 | + |
| 93 | + public DateAndTime(final OffsetDateTime dateTime) { |
| 94 | + this.dateTime = dateTime; |
| 95 | + } |
| 96 | + |
| 97 | + public DateAndTime(final Instant localDateTime, final ZoneOffset offset) { |
| 98 | + this.dateTime = localDateTime.atOffset( offset ); |
| 99 | + } |
| 100 | + |
| 101 | + public LocalDateTime getLocalDateTime() { |
| 102 | + return dateTime.toLocalDateTime(); |
| 103 | + } |
| 104 | + |
| 105 | + public void setLocalDateTime(final LocalDateTime localDateTime) { |
| 106 | + this.dateTime = localDateTime.atOffset( this.dateTime.getOffset() ); |
| 107 | + } |
| 108 | + |
| 109 | + public ZoneOffset getOffset() { |
| 110 | + return dateTime.getOffset(); |
| 111 | + } |
| 112 | + |
| 113 | + public void setOffset(final ZoneOffset offset) { |
| 114 | + this.dateTime = dateTime.toLocalDateTime().atOffset( offset ); |
| 115 | + } |
| 116 | + |
| 117 | + @Transient |
| 118 | + public OffsetDateTime dateTime() { |
| 119 | + return dateTime; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments