关于hibernate的加载方式有两种,一种是get加载,一种是load加载,load属于延迟加载,使用了动态代理。这不是我们所关心的,我们看一看一对一双向关联在加载时有什么特点,想要观察特点,不得不看hibernate为我们生成的sql语句。 husband类与上一篇没有变化
关于hibernate的加载方式有两种,一种是get加载,一种是load加载,load属于延迟加载,使用了动态代理。这不是我们所关心的,我们看一看一对一双向关联在加载时有什么特点,想要观察特点,不得不看hibernate为我们生成的sql语句。
husband类与上一篇没有变化如下:
/** * */ package com.maybe.test_1; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; /** * @author MayBe * * function: */ @Entity @Table(name="t_husband") public class Husband { private Integer id; private String name; private Wife wife; @Id @GeneratedValue public Integer getId() { return id; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } @OneToOne(mappedBy="husband") public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; } }
wife类也没有变化:
/** * */ package com.maybe.test_1; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; /** * @author MayBe * * function: */ @Entity @Table(name="t_wife") public class Wife { private Integer id; private String name; private Husband husband; @Id @GeneratedValue public Integer getId() { return id; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } @OneToOne public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } }
junit测试方法如下:
@Test public void One_to_oneLoadTest1(){ //一对一双向关联 Session s = sessionFacotry.getCurrentSession(); s.beginTransaction(); Husband hus = (Husband)s.get(Husband.class, 2); System.out.println("*****************"); Wife wif = (Wife)s.get(Wife.class, 3); s.getTransaction().commit(); }
假设我们在数据中存在一个id为2的husband,他与id为2的wife是关联的。在数据库有一个id为3的wife,她与id为3的husband是关联的,我们运行一下这个测试,输出sql语句如下:
2014-02-04 16:52:20 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update 2014-02-04 16:52:20 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:193 - HHH000102: Fetching database metadata 2014-02-04 16:52:20 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:205 - HHH000396: Updating schema 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_husband 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, name] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: [] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_husban__3213e83f0f624af8] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_wife 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, husband_id, name] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: [fk_fi3kodkmubgryyblf4935y4dk] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_wife__3213e83f1332dbdc] 2014-02-04 16:52:21 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:242 - HHH000232: Schema update complete Hibernate: select husband0_.id as id1_0_0_, husband0_.name as name2_0_0_, wife1_.id as id1_1_1_, wife1_.husband_id as husband_3_1_1_, wife1_.name as name2_1_1_ from t_husband husband0_ left outer join t_wife wife1_ on husband0_.id=wife1_.husband_id where husband0_.id=? ***************** Hibernate: select wife0_.id as id1_1_0_, wife0_.husband_id as husband_3_1_0_, wife0_.name as name2_1_0_, husband1_.id as id1_0_1_, husband1_.name as name2_0_1_ from t_wife wife0_ left outer join t_husband husband1_ on wife0_.husband_id=husband1_.id where wife0_.id=? Hibernate: select wife0_.id as id1_1_1_, wife0_.husband_id as husband_3_1_1_, wife0_.name as name2_1_1_, husband1_.id as id1_0_0_, husband1_.name as name2_0_0_ from t_wife wife0_ left outer join t_husband husband1_ on wife0_.husband_id=husband1_.id where wife0_.husband_id=?
首先加载husband对象时会把相关联的字段全部加载出来。
但是可以看出来加载wife表的时候会执行两次查询,虽然这两次查询的内容是一样的,但是第一次是查询出的是主表的信息,第二次是为了查出从表信息,这是hibernate的默认加载策略,因为t_wife表有外键,如果加载的表有从表,他会默认加载同时夹在两个表的信息。是否加载从表信息是由fetch进行设置的。
我们把wife类one-to-one新添加一个属性
/** * */ package com.maybe.test_1; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; /** * @author MayBe * * function: */ @Entity @Table(name="t_wife") public class Wife { private Integer id; private String name; private Husband husband; @Id @GeneratedValue public Integer getId() { return id; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } @OneToOne(fetch=FetchType.LAZY) public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } }
我们把抓取方式改成懒抓取,加载输出如下所示:
2014-02-04 17:07:38 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update
2014-02-04 17:07:38 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:193 - HHH000102: Fetching database metadata
2014-02-04 17:07:38 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:205 - HHH000396: Updating schema
2014-02-04 17:07:39 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_husband
2014-02-04 17:07:39 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, name]
2014-02-04 17:07:39 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: []
2014-02-04 17:07:39 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_husban__3213e83f0f624af8]
2014-02-04 17:07:40 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_wife
2014-02-04 17:07:40 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, husband_id, name]
2014-02-04 17:07:40 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: [fk_fi3kodkmubgryyblf4935y4dk]
2014-02-04 17:07:40 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_wife__3213e83f1332dbdc]
2014-02-04 17:07:40 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:242 - HHH000232: Schema update complete
Hibernate:
select
husband0_.id as id1_0_0_,
husband0_.name as name2_0_0_,
wife1_.id as id1_1_1_,
wife1_.husband_id as husband_3_1_1_,
wife1_.name as name2_1_1_
from
t_husband husband0_
left outer join
t_wife wife1_
on husband0_.id=wife1_.husband_id
where
husband0_.id=?
*****************
Hibernate:
select
wife0_.id as id1_1_0_,
wife0_.husband_id as husband_3_1_0_,
wife0_.name as name2_1_0_
from
t_wife wife0_
where
wife0_.id=?
这样wife类只加载自己主表的信息,不考虑从表的信息了( 当然把husband也是如此,可以自己去试试)。
hibernate默认的加载策略为eager加载,也就是急抓取,会把所关联的信息一次性全部得到,而lazy则都是在你用到从表才会去加载。
我们改变一下测试类内容:
public void One_to_oneLoadTest1() { // 一对一双向关联 Session s = sessionFacotry.getCurrentSession(); s.beginTransaction(); Husband hus = (Husband) s.get(Husband.class, 2); System.out.println("*****************"); Wife wif = (Wife) s.get(Wife.class, 3); System.out.println(wif.getHusband().getName()); s.getTransaction().commit(); }
输出如下:
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:193 - HHH000102: Fetching database metadata
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:205 - HHH000396: Updating schema
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_husband
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, name]
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: []
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_husban__3213e83f0f624af8]
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:66 - HHH000261: Table found: hibernate.dbo.t_wife
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:67 - HHH000037: Columns: [id, husband_id, name]
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:69 - HHH000108: Foreign keys: [fk_fi3kodkmubgryyblf4935y4dk]
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.TableMetadata:70 - HHH000126: Indexes: [pk__t_wife__3213e83f1332dbdc]
2014-02-04 17:10:51 INFO org.hibernate.tool.hbm2ddl.SchemaUpdate:242 - HHH000232: Schema update complete
Hibernate:
select
husband0_.id as id1_0_0_,
husband0_.name as name2_0_0_,
wife1_.id as id1_1_1_,
wife1_.husband_id as husband_3_1_1_,
wife1_.name as name2_1_1_
from
t_husband husband0_
left outer join
t_wife wife1_
on husband0_.id=wife1_.husband_id
where
husband0_.id=?
*****************
Hibernate:
select
wife0_.id as id1_1_0_,
wife0_.husband_id as husband_3_1_0_,
wife0_.name as name2_1_0_
from
t_wife wife0_
where
wife0_.id=?
Hibernate:
select
husband0_.id as id1_0_0_,
husband0_.name as name2_0_0_,
wife1_.id as id1_1_1_,
wife1_.husband_id as husband_3_1_1_,
wife1_.name as name2_1_1_
from
t_husband husband0_
left outer join
t_wife wife1_
on husband0_.id=wife1_.husband_id
where
husband0_.id=?
Hibernate:
select
wife0_.id as id1_1_0_,
wife0_.husband_id as husband_3_1_0_,
wife0_.name as name2_1_0_
from
t_wife wife0_
where
wife0_.husband_id=?
GossipMan
这样才会加载从表的信息,对于一些其他的语句,可能是hibernate自动生成的其他语句,但只要理解其中的思想就可以了,不要在意这些细节。

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

ToaddauserinMySQL,usetheCREATEUSERstatement.1)UseCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';tocreateauser.2)Enforcestrongpasswordpolicieswithvalidate_passwordpluginsettings.3)GrantspecificprivilegesusingGRANTstatement.4)Forremoteaccess,use

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
