上記参照:ウィンドウ内のOracle XEのインストール11グラム
EclipseでのHibernateを使用
インストールHibernateツールプラグイン
https://tools.jboss.org/downloads/
、あなたのEclipse 4.13(2019から09)インストールするには、次のURLを経由して追加します。
ヘルプ>新規ソフトウェアのインストール...>との仕事:
http://download.jboss.org/jbosstools/photon/stable/updates/
その後、インストールする個々の機能を選択します。
[次へ]をクリックします
[次へ]をクリックして
契約に同意し、[完了]をクリックします。
これは、ダウンロードしてインストールを開始します。
ネットワークの速度によっては、インストールを完了するために10分には数分かかることがあります。
最後に、あなたを有効にするにはEclipseを再起動するよう求められます。
Eclipseで新しいHibernateのアプリケーション
ファイル - >新規 - > Javaプロジェクト
[完了]をクリックします
プログラム構造。
Eclipseで新しいユーザ・ライブラリ
この時点で、ユーザーのライブラリー・リストは以下の通りです設立されました
我々は、Hibernateの依存関係ライブラリーを追加したい、ユーザライブラリをクリックしてください
Hibernate_4.3.5_final
jarファイルを選択します
プログラム構造。
休止状態の設定を続行
最後に、ドキュメントの次の内容を自動的に形成する:[この例では、Oracleデータベースを使用します]
WindowsのインストールでOracle 11グラムXEは、以下のリンクを参照してください。
hibernate.cfg.xmlの
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
</session-factory>
</hibernate-configuration>
そして、いくつかの属性を追加します
注nameプロパティの変更を削除するには次のように設定ファイルの内容が更新された後、
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:xe:orcl</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
</session-factory>
</hibernate-configuration>
エンジニアリングHibernate_demo_001を継続的に改善します
mytest001.demo:新しいパッケージを作成します。
新しいパケットmytest001.demoのPOクラスの下で:emp表
package mytest001.demo;
public class Emp {
// 员工的标识属性
private Integer id;
// 姓名
private String name;
// 年龄
private Integer age;
// 工资 (分)
private Integer salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
現時点ではこのPO Emp.javaは永続能力を持っていません。以下のコメントを追加します。
@Entity注釈クラス宣言が休止永続クラスである
@Tableは、クラスマッピングテーブル指定し、対応するデータベーステーブル名がT_EMPある
@Idは、識別属性クラスを指定し、主キー列は、データベースにマップされ
@GeneratedValue(戦略= GenerationType。 SEQUENCE)は本明細書で使用される、主キーの生成方法を指定すると、Oracle Database、SEQUENCEの使用を指定するので、
hibernate.cfg.xmlでクラス名をマッピング永続性を上げます
管理従業員のために、EmpManager:新しいカテゴリを追加します。
package mytest001.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class EmpManager {
public static void main(String[] args) throws Exception {
//实例化配置
Configuration configuration = new Configuration()
//不带参数则默认加载hibernate.cfg.xml
.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sFactory = configuration.buildSessionFactory(serviceRegistry);
//创建session
Session session = sFactory.openSession();
//开始事务
Transaction tx = session.beginTransaction();
//创建员工对象
Emp emp = new Emp();
// 设置员工信息
emp.setAge(28);
emp.setName("scott");
emp.setSalary(10000);
session.save(emp);
// 提交事务
tx.commit();
session.close();
sFactory.close();
}
}
最後に、設定ファイルの内容:hibernate.cfg.xmlの
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<mapping class="mytest001.demo.Emp"/>
</session-factory>
</hibernate-configuration>
Emp.java
package mytest001.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="T_EMP")
public class Emp {
// 员工的标识属性
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer id;
// 姓名
private String name;
// 年龄
private Integer age;
// 工资 (分)
private Integer salary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
次のように最終的なプロジェクトが構成されています。
ファイル名を指定して実行EmpManger
十一月 23, 2019 8:50:47 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
十一月 23, 2019 8:50:47 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
十一月 23, 2019 8:50:47 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十一月 23, 2019 8:50:47 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十一月 23, 2019 8:50:47 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十一月 23, 2019 8:50:47 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十一月 23, 2019 8:50:47 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十一月 23, 2019 8:50:47 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十一月 23, 2019 8:50:47 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:xe]
十一月 23, 2019 8:50:47 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=test, password=****}
十一月 23, 2019 8:50:47 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十一月 23, 2019 8:50:47 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十一月 23, 2019 8:50:48 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
十一月 23, 2019 8:50:48 上午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十一月 23, 2019 8:50:48 上午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: T_EMP
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: T_EMP
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: T_EMP
十一月 23, 2019 8:50:48 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate:
select
test.hibernate_sequence.nextval
from
dual
Hibernate:
insert
into
test.T_EMP
(age, name, salary, id)
values
(?, ?, ?, ?)
十一月 23, 2019 8:50:48 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521:xe]
データベーステーブルへのクエリ:(このテーブルが自動的に作成される)
T_EMPからSELECT *;
再度実行すると、新しいレコードを追加します。
これまでのところ、この記事で完了です。