<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Hibernate `@NaturalId`示例教程 > 原文: [https://howtodoinjava.com/hibernate/hibernate-naturalid-example-tutorial/](https://howtodoinjava.com/hibernate/hibernate-naturalid-example-tutorial/) [**Hibernate**](//howtodoinjava.com/hibernate-tutorials/ "hibernate tutorial") 4 帶來了許多改進, [**`@NaturalId`**](http://docs.jboss.org/hibernate/orm/4.0/javadocs/org/hibernate/annotations/NaturalId.html "NaturalId")就是這樣的改進之一。 如您所知,`@Id`注解用作指定實體主鍵的元數據。 但是有時,實體通常在 DAO 層代碼中使用 ID,該 ID 不是主鍵,而是其邏輯或自然 ID。 在這種情況下,`@NaturalId`注解將證明是 Hiberate 下命名查詢的良好替代。 例如,在任何應用中都可以有一個雇員實體。 在這種情況下,主鍵肯定是“員工 ID”,但在通過電子郵件登錄等情況下,用戶將提供電子郵件和密碼。 在這種情況下,您可以直接在“電子郵件”字段上使用`@NaturalId`注解,而不必編寫命名查詢。 ## 工作原理 如果您查看日志,那么您會知道,當您通過其自然 ID 獲得實體時, 1. 通過執行自然 ID 的`where`子句找到實體的第一個主鍵 2. 該主鍵用于獲取實體的信息 ## 用法示例 讓我們來看這個例子。 作為參考,最新的 Hiberate Maven 依賴項如下: ```java <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.0.Beta3</version> </dependency> ``` 現在,最小員工實體應如下所示: **`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); EmployeeEntity empGet = (EmployeeEntity) session.bySimpleNaturalId( EmployeeEntity.class ).load( "demo-user@mail.com" ); 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 (?, ?, ?) Hibernate: select employeeen_.ID as ID1_0_ from Employee employeeen_ where employeeen_.EMAIL=? 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>

                              哎呀哎呀视频在线观看