返回介绍

尝试一下

发布于 2025-04-21 21:42:14 字数 9742 浏览 0 评论 0 收藏 0

一切准备好以后,再回到第 2.3 节建立的那个数据库模式创建的示例。这一次它应该在 MySQL 服务器上创建数据库模式,而不是在嵌入式 HSQLDB 中。数据库模式创建的输出如例 10-4 所示,不过,实际输出的内容要比这个例子列举的内容还要长,所以就删除了部分重复和繁琐的输出,以突出一些特别重要的片段,通过它们来证明我们的新设置已经生效了(注意,我们以一个单独的 ant prepare 调用作为开始,因为这是我们第一次在本章的示例目录运行程序,在 Ant 第一次开始运行 schema 构建目标时,我们需要将正确的文件放在类路径上)。

例 10-4:连接 MySQL 并创建数据库模式

%ant prepare

Buildfile:build.xml

Downloading:mysql/mysql-connector-java/5.0.5/mysql-connector-java-5.0.5.pom

Transferring 1K

Downloading:mysql/mysql-connector-java/5.0.5/mysql-connector-java-5.0.5.jar

Transferring 500K

prepare:

[mkdir]Created dir:/Users/jim/svn/oreilly/hibernate/current/examples

/ch10/classes

[copy]Copying 5 files to/Users/jim/svn/oreilly/hibernate/current

/examples/ch10/classes

BUILD SUCCESSFUL

Total time:3 seconds

%ant schema

Buildfile:build.xml

prepare:

usertypes:

[javac]Compiling 2 source files to/Users/jim/svn/oreilly/hibernate

/current/examples/ch10/classes

codegen:

[hibernatetool]Executing Hibernate Tool with a Standard Configuration

[hibernatetool]1.task:hbm2java(Generates a set of.java files)

[hibernatetool]16:46:38,403 INFO Environment:514-Hibernate 3.2.5

[hibernatetool]16:46:38,415 INFO Environment:547-hibernate.properties

not found

[hibernatetool]16:46:38,419 INFO Environment:681-Bytecode provider name

:cglib

[hibernatetool]16:46:38,434 INFO Environment:598-using JDK 1.4 java.

sql.Timestamp handling

[hibernatetool]16:46:38,561 INFO Configuration:1460-configuring from

file:hibernate.cfg.xml

[hibernatetool]16:46:38,740 INFO Configuration:553-Reading mappings

from resource:com/oreilly/hh/data/Track.hbm.xml

[hibernatetool]16:46:38,932 INFO HbmBinder:300-Mapping class:com

.oreilly.hh.data.Track->TRACK

……

[hibernatetool]16:46:39,110 INFO HbmBinder:1422-Mapping collection:

com.oreilly.hh.data.Artist.tracks->TRACK_ARTISTS

[hibernatetool]16:46:39,239 INFO Configuration:1541-Configured SessionFactor

y:null

[hibernatetool]16:46:39,411 INFO Version:15-Hibernate Tools 3.2.0.b9

compile:

[javac]Compiling 9 source files to/Users/jim/svn/oreilly/hibernate

/current/examples/ch10/classes

schema:

[hibernatetool]Executing Hibernate Tool with a Standard Configuration

[hibernatetool]1.task:hbm2ddl(Generates database schema)

[hibernatetool]16:46:41,502 INFO Configuration:1460-configuring from

file:hibernate.cfg.xml

[hibernatetool]16:46:41,515 INFO Configuration:553-Reading mappings

from resource:com/oreilly/hh/data/Track.hbm.xml

……

[hibernatetool]16:46:41,629 INFO Configuration:1541-Configured SessionFactor

y:null

[hibernatetool]16:46:41,659 INFO Dialect:152-Using dialect:org.hibernate

.dialect.MySQL5Dialect

[hibernatetool]16:46:41,714 INFO SchemaExport:154-Running hbm2ddl

schema export

[hibernatetool]16:46:41,716 INFO SchemaExport:179-exporting generated

schema to database

[hibernatetool]16:46:41,725 INFO DriverManagerConnectionProvider:41-Using

Hibernate built-in connection pool(not for production use!)

[hibernatetool]16:46:41,726 INFO DriverManagerConnectionProvider:42-Hibernat

e connection pool size:1

[hibernatetool]16:46:41,727 INFO DriverManagerConnectionProvider:45-autocomm

it mode:false

[hibernatetool]16:46:41,745 INFO DriverManagerConnectionProvider:80-using

driver:com.mysql.jdbc.Driver at URL:jdbc:mysql://localhost/notebook_db

[hibernatetool]16:46:41,746 INFO DriverManagerConnectionProvider:86-

connection properties:{user=jim, password=****,shutdown=true}

[hibernatetool]alter table ALBUM_ARTISTS drop foreign key FK7BA403FC76BBFFF9;

……

[hibernatetool]alter table TRACK_COMMENTS drop foreign key FK105B2688E424525B;

[hibernatetool]drop table if exists ALBUM;

[hibernatetool]drop table if exists ALBUM_ARTISTS;

[hibernatetool]drop table if exists ALBUM_COMMENTS;

[hibernatetool]drop table if exists ALBUM_TRACKS;

[hibernatetool]drop table if exists ARTIST;

[hibernatetool]drop table if exists TRACK;

[hibernatetool]drop table if exists TRACK_ARTISTS;

[hibernatetool]drop table if exists TRACK_COMMENTS;

[hibernatetool]create table ALBUM(ALBUM_ID integer not null auto_increment, TI

TLE varchar(255)not null, numDiscs integer, added date, primary key(ALBUM_ID))

[hibernatetool]create table ALBUM_ARTISTS(ALBUM_ID integer not null, ARTIST_ID

integer not null, primary key(ALBUM_ID, ARTIST_ID));

[hibernatetool]create table ALBUM_COMMENTS(ALBUM_ID integer not null, COMMENT

varchar(255));

[hibernatetool]create table ALBUM_TRACKS(ALBUM_ID integer not null, TRACK_ID

integer, disc integer, positionOnDisc integer, LIST_POS integer not null, primary

key(ALBUM_ID, LIST_POS));

[hibernatetool]create table ARTIST(ARTIST_ID integer not null auto_increment,

NAME varchar(255)not null unique, actualArtist integer, primary key(ARTIST_ID)

);

[hibernatetool]create table TRACK(TRACK_ID integer not null auto_increment, TI

TLE varchar(255)not null, filePath varchar(255)not null, playTime time, added

date, VOL_LEFT smallint, VOL_RIGHT smallint, sourceMedia varchar(255),

primary key(TRACK_ID));

[hibernatetool]create table TRACK_ARTISTS(TRACK_ID integer not null, ARTIST_ID

integer not null, primary key(TRACK_ID, ARTIST_ID));

[hibernatetool]create table TRACK_COMMENTS(TRACK_ID integer not null, COMMENT

varchar(255));

[hibernatetool]create index ALBUM_TITLE on ALBUM(TITLE);

[hibernatetool]alter table ALBUM_ARTISTS add index FK7BA403FC76BBFFF9(ARTIST_ID),

add constraint FK7BA403FC76BBFFF9 foreign key(ARTIST_ID)references ARTIST

(ARTIST_ID);

[hibernatetool]alter table ALBUM_ARTISTS add index FK7BA403FCF2AD8FDB(ALBUM_ID),

add constraint FK7BA403FCF2AD8FDB foreign key(ALBUM_ID)references ALBUM

(ALBUM_ID);

[hibernatetool]alter table ALBUM_COMMENTS add index FK1E2C21E4F2AD8FDB

(ALBUM_ID),add constraint FK1E2C21E4F2AD8FDB foreign key(ALBUM_ID)references

ALBUM(ALBUM_ID);

[hibernatetool]alter table ALBUM_TRACKS add index FKD1CBBC78E424525B

(TRACK_ID),add constraint FKD1CBBC78E424525B foreign key(TRACK_ID)references

TRACK(TRACK_ID);

[hibernatetool]alter table ALBUM_TRACKS add index FKD1CBBC78F2AD8FDB

(ALBUM_ID),add constraint FKD1CBBC78F2AD8FDB foreign key(ALBUM_ID)references

ALBUM(ALBUM_ID);

[hibernatetool]create index ARTIST_NAME on ARTIST(NAME);

[hibernatetool]alter table ARTIST add index FK7395D347A1422D3B(actualArtist),

add constraint FK7395D347A1422D3B foreign key(actualArtist)references

ARTIST(ARTIST_ID);

[hibernatetool]create index TRACK_TITLE on TRACK(TITLE);

[hibernatetool]alter table TRACK_ARTISTS add index FK72EFDAD8E424525B

(TRACK_ID),add constraint FK72EFDAD8E424525B foreign key(TRACK_ID)references

TRACK(TRACK_ID);

[hibernatetool]alter table TRACK_ARTISTS add index FK72EFDAD876BBFFF9

(ARTIST_ID),add constraint FK72EFDAD876BBFFF9 foreign key(ARTIST_ID)

references ARTIST(ARTIST_ID);

[hibernatetool]alter table TRACK_COMMENTS add index FK105B2688E424525B

(TRACK_ID),add constraint FK105B2688E424525B foreign key(TRACK_ID)references

TRACK(TRACK_ID);

[hibernatetool]16:46:42,630 INFO SchemaExport:196-schema export complete

[hibernatetool]16:46:42,631 INFO DriverManagerConnectionProvider:147-cleanin

g up connection pool:jdbc:mysql://localhost/notebook_db

[hibernatetool]9 errors occurred while performing<hbm2ddl>.

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.album_artists'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.album_artists'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.album_comments'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.album_tracks'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.album_tracks'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.artist'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.track_artists'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.track_artists'doesn't exist

[hibernatetool]Error#1:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:T

able'notebook_db.track_comments'doesn't exist

BUILD SUCCESSFUL

Total time:8 seconds

发生了什么事

Hibernate 会自行配置和使用 MySQL 的特定功能,检查我们的 Track 类的映射文档,连接到 MySQL 服务器,执行必要的命令以创建供持久化我们的示例数据所需要的数据库模式(和以前一样,最后会报告几个 SQL 异常,你可以忽略它们;这些异常可能是因为 Hibernate 试图删除并不存在的外键而引起的,因为这是我们首次尝试创建数据库模式)。

再次说明一下,不必担心最后出现的错误;这些错误是由于为了防止已经存在部分数据库模式,Hibernate 抢先试图删除它们而造成的。即便报告有错误,Hibernate 并不认为它们会造成问题,所以继续处理剩下的数据库模式创建任务,并最终报告操作成功完成。

把这个示例和 HSQLDB 版本的数据库模式创建(例 2-7)进行比较,结果会很有趣。它们的输出差不多是相同的,但是用于创建数据库表的 SQL 语句明显不同。这就是 Hibernate 中所谓的 SQL“方言”。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。