|
| 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.test.bytecode.enhancement.lazy; |
| 8 | + |
| 9 | + |
| 10 | +import javax.persistence.Basic; |
| 11 | +import javax.persistence.FetchType; |
| 12 | +import javax.persistence.GeneratedValue; |
| 13 | +import javax.persistence.Id; |
| 14 | + |
| 15 | +import org.hibernate.Hibernate; |
| 16 | +import org.hibernate.Session; |
| 17 | +import org.hibernate.cfg.Configuration; |
| 18 | +import org.hibernate.cfg.Environment; |
| 19 | + |
| 20 | +import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask; |
| 21 | +import org.hibernate.test.bytecode.enhancement.EnhancerTestUtils; |
| 22 | +import org.junit.Assert; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Gail Badner |
| 26 | + */ |
| 27 | +public class LazyBasicFieldNotInitializedTestTask extends AbstractEnhancerTestTask { |
| 28 | + |
| 29 | + private Long entityId; |
| 30 | + |
| 31 | + public Class<?>[] getAnnotatedClasses() { |
| 32 | + return new Class<?>[] {Entity.class}; |
| 33 | + } |
| 34 | + |
| 35 | + public void prepare() { |
| 36 | + Configuration cfg = new Configuration(); |
| 37 | + cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); |
| 38 | + cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" ); |
| 39 | + super.prepare( cfg ); |
| 40 | + |
| 41 | + Session s = getFactory().openSession(); |
| 42 | + s.beginTransaction(); |
| 43 | + |
| 44 | + Entity entity = new Entity(); |
| 45 | + entity.setDescription( "desc" ); |
| 46 | + s.persist( entity ); |
| 47 | + entityId = entity.getId(); |
| 48 | + |
| 49 | + s.getTransaction().commit(); |
| 50 | + s.clear(); |
| 51 | + s.close(); |
| 52 | + } |
| 53 | + |
| 54 | + public void execute() { |
| 55 | + Session s = getFactory().openSession(); |
| 56 | + s.beginTransaction(); |
| 57 | + Entity entity = s.get( Entity.class, entityId ); |
| 58 | + Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) ); |
| 59 | + s.getTransaction().commit(); |
| 60 | + s.close(); |
| 61 | + } |
| 62 | + |
| 63 | + protected void cleanup() { |
| 64 | + } |
| 65 | + |
| 66 | + @javax.persistence.Entity |
| 67 | + public static class Entity { |
| 68 | + @Id |
| 69 | + @GeneratedValue |
| 70 | + private Long id; |
| 71 | + |
| 72 | + @Basic(fetch = FetchType.LAZY) |
| 73 | + private String description; |
| 74 | + |
| 75 | + public Long getId() { |
| 76 | + return id; |
| 77 | + } |
| 78 | + |
| 79 | + public void setId(Long id) { |
| 80 | + this.id = id; |
| 81 | + } |
| 82 | + |
| 83 | + public String getDescription() { |
| 84 | + return description; |
| 85 | + } |
| 86 | + |
| 87 | + public void setDescription(String description) { |
| 88 | + this.description = description; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +} |
0 commit comments