Assume that there is a bidirectional one-to-multiple association between the Customer class and the Order class.
Attributes of the Customer class:
[Java]
/** Object OID */
Private Long id;
/** Name of the Customer object */
Private String name;
/** Age of the Customer object */
Private int age;
/** Order with Customer object */
Private Set <Order> orders = new HashSet <Order> ();
Hibernate ing file:
[Html]
<Class name = "Customer" table = "customers">
<Id name = "id" type = "long" column = "id">
<Generator class = "native"/>
</Id>
<Property name = "name" type = "string" column = "name"/>
<Property name = "age" type = "int" column = "age"/>
<Set name = "orders" cascade = "all" inverse = "true" lazy = "true">
<Key column = "customer_id"/>
<One-to-define class = "Order"/>
</Set>
</Class>
Attributes of the Order class:
[Java]
/** Object OID */
Private Long id;
/** Order Number of the Order object */
Private String orderNumber;
/** Order price of the Order object */
Private double price;
/** The Customer object associated with the Order object */
Private Customer customer;
Hibernate ing file:
[Html]
<Class name = "Order" table = "orders">
<Id name = "id" type = "long" column = "id">
<Generator class = "native"/>
</Id>
<Property name = "orderNumber" type = "string" column = "orderNumber" length = "15"/>
<Property name = "price" type = "double" column = "price"/>
<Role-to-one name = "customer" column = "customer_id" class = "Customer"/>
</Class>
Assume that the data in the database is:
Cusomters table
Orders table:
The following is the test code:
[Java]
List result = session. createQuery ("select c from Customer as c inner join c. orders where c. name like't % '")
. List ();
Iterator it = result. iterator ();
Customer customer = (Customer) it. next ();
System. out. println (customer. getOrders (). size ());
Hibernate generates the following SQL statement:
[SQL]
Hibernate:
Select
Customer0 _. id as id4 _,
Customer0 _. name as name4 _,
Customer0 _. age as age4 _
From
Customers customer0 _
Inner join
Orders orders1 _
On customer0 _. id = orders1 _. customer_id
Where
Customer0 _. name like't %'
Hibernate:
Select
Orders0 _. customer_id as customer4_4_1 _, // repeated field
Orders0 _. id as id1 _, // duplicate Field
Orders0 _. id as id5_0 _,
Orders0 _. orderNumber as orderNum2_5_0 _,
Orders0 _. price as price5_0 _,
Orders0 _. customer_id as customer4_5_0 _
From
Orders orders0 _
Where
Orders0 _. customer_id =?
Question:
In the preceding SQL statement, duplicate comment fields are repeated with the following fields. Why do we need to repeat the primary key id and customer_id fields of orders?
In addition:
The above uses the internal Link Method for retrieval, but in fact, the external connection is urgently used for external connections. In a single table query, the primary key and foreign key fields are also retrieved repeatedly.