<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Hibernate 4 – 獲取延遲加載的實體引用 > 原文: [https://howtodoinjava.com/hibernate/hibernate-4-example-to-get-entity-reference-for-lazy-loading/](https://howtodoinjava.com/hibernate/hibernate-4-example-to-get-entity-reference-for-lazy-loading/) 根據 Wikipedia 的定義, [**延遲加載**](https://en.wikipedia.org/wiki/Lazy_loading "lazy loading")是一種設計模式,通常用于計算機編程中,以將對象的初始化推遲到需要的時間點。 我們知道,在 Hiberate 映射中,可以通過在 Hiberate 映射注解中指定`fetch = FetchType.LAZY`來完成延遲加載。 例如: ```java @ManyToOne ( fetch = FetchType.LAZY ) @JoinColumns( { @JoinColumn(name="fname", referencedColumnName = "firstname"), @JoinColumn(name="lname", referencedColumnName = "lastname") } ) public EmployeeEntity getEmployee() { return employee; } ``` 關鍵是僅在定義兩個實體之間的映射時才應用它。 如果已經在`DepartmentEntity`中定義了以上實體,則如果您獲取`DepartmentEntity`,則`EmployeeEntity`將被延遲加載。 但是,如果您要延遲加載`DepartmentEntity`本身,即**主實體本身應延遲加載**,該怎么辦。 可以通過在`IdentifierLoadAccess`類中使用[**`getReference()`**](http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/IdentifierLoadAccess.html#getReference%28java.io.Serializable%29 "IdentifierLoadAccess")方法解決此問題。 讓我們通過此示例了解用法。 作為參考,最新的 Hiberate Maven 依賴項如下: ```java <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.0.Beta3</version> </dependency> ``` 現在,我有一個主實體類`EmployeeEntity`,它可以具有多個屬性以及與其他實體的映射。 **`EmployeeEntity.java`** ```java @Entity @Table(name = "Employee", uniqueConstraints = { @UniqueConstraint(columnNames = "ID"), @UniqueConstraint(columnNames = "EMAIL") }) public class EmployeeEntity implements Serializable { private static final long serialVersionUID = -1798070786993154676L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Integer employeeId; //Use the natural id annotation here @NaturalId (mutable = false) @Column(name = "EMAIL", unique = true, nullable = false, length = 100) private String email; @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100) private String firstName; @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100) private String lastName; //Setters and Getters } ``` 我想在我的代碼中延遲加載的主實體之上延遲加載,即我可以在一個地方獲取實體的引用,但實際上可能需要在另一個地方進行引用。 僅在需要時,我才想初始化或加載其數據。 到時候為止,我只想參考。 讓我們在代碼示例中這樣做: **`TestHibernate.java`** ```java public class TestHibernate { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //Add new Employee object EmployeeEntity emp = new EmployeeEntity(); emp.setEmail("demo-user@mail.com"); emp.setFirstName("demo"); emp.setLastName("user"); //Save entity session.save(emp); session.getTransaction().commit(); session.close(); session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //Get only the reference of EmployeeEntity for now EmployeeEntity empGet = (EmployeeEntity) session.byId( EmployeeEntity.class ).getReference( 1 ); System.out.println("No data initialized till now; Lets fetch some data.."); //Now EmployeeEntity will be loaded from database when we need it System.out.println(empGet.getFirstName()); System.out.println(empGet.getLastName()); session.getTransaction().commit(); HibernateUtil.shutdown(); } } Output in console: Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?) No data initialized till now; Lets fetch some data.. Hibernate: select employeeen0_.ID as ID1_0_0_, employeeen0_.EMAIL as EMAIL2_0_0_, employeeen0_.FIRST_NAME as FIRST3_0_0_, employeeen0_.LAST_NAME as LAST4_0_0_ from Employee employeeen0_ where employeeen0_.ID=? demo user ``` 要下載以上教程的源代碼,請單擊下面的下載鏈接。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看