|
| 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.bytecode.enhancement.metamodel; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.metamodel.Attribute; |
| 10 | +import jakarta.persistence.metamodel.ManagedType; |
| 11 | +import jakarta.persistence.metamodel.Metamodel; |
| 12 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 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 java.lang.reflect.Member; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Vector; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | + /** |
| 25 | + * @author Scott Marlow |
| 26 | + */ |
| 27 | + @BytecodeEnhanced |
| 28 | + @SessionFactory |
| 29 | + public class MetamodelJavaTypeTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + @DomainModel( annotatedClasses = SimpleEntity.class ) |
| 33 | + public void basicManagedTest(SessionFactoryScope scope) { |
| 34 | + SimpleEntity entity = new SimpleEntity(); |
| 35 | + scope.inTransaction( entityManager -> { |
| 36 | + entityManager.persist( entity ); |
| 37 | + Metamodel metaModel = entityManager.getMetamodel(); |
| 38 | + ManagedType<SimpleEntity> managedType = metaModel.managedType( SimpleEntity.class ); |
| 39 | + Attribute<SimpleEntity,?> attribute = managedType.getDeclaredAttribute( "total" ); |
| 40 | + Member member = attribute.getJavaMember(); |
| 41 | + assertEquals( "getTotal", member.getName() ); |
| 42 | + } ); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + // --- // |
| 47 | + |
| 48 | + @Entity |
| 49 | + private static class SimpleEntity { |
| 50 | + |
| 51 | + int id; |
| 52 | + int total; |
| 53 | + |
| 54 | + Collection itemNames = new Vector(); |
| 55 | + |
| 56 | + public SimpleEntity() { |
| 57 | + } |
| 58 | + |
| 59 | + public SimpleEntity(int total) { |
| 60 | + this.total = total; |
| 61 | + } |
| 62 | + |
| 63 | + public SimpleEntity(int id, int total) { |
| 64 | + this.total = total; |
| 65 | + this.id = id; |
| 66 | + } |
| 67 | + |
| 68 | + @Id |
| 69 | + public int getId() { |
| 70 | + return id; |
| 71 | + } |
| 72 | + |
| 73 | + public void setId(int id) { |
| 74 | + this.id = id; |
| 75 | + } |
| 76 | + |
| 77 | + public int getTotal() { |
| 78 | + return total; |
| 79 | + } |
| 80 | + |
| 81 | + public void setTotal(int total) { |
| 82 | + this.total = total; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
0 commit comments