下面是我的两个映射文件:
Blog:
<class name="Blog" table="blog">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="title" type="string" column="title"></property>
<property name="content" type="string" column="content"></property>
<property name="datetime" type="date" column="createTime"></property>
<set name="comments" inverse="true" cascade="delete">
<key column="blogId"></key>
<one-to-many class="Comment"/>
</set>
</class>
Comment:
<class name="Comment" table="comment">
<id name="id" column="id">
<generator class="identity"></generator>
</id>
<property name="user" type="string" column="user"></property>
<property name="email" type="string" column="email"></property>
<property name="url" type="string" column="url"></property>
<property name="content" type="string" column="content"></property>
<property name="datetime" type="date" column="createTime"></property>
<many-to-one name="blog" class="Blog" column="blogId" not-null="true"></many-to-one>
</class>
当我运行程序的时候打印出来hibernate执行的sql语句是这样的:
Hibernate:
alter table comment
drop
foreign key FK38A5EE5FDE0F1321
Hibernate:
drop table if exists blog
Hibernate:
drop table if exists comment
Hibernate:
create table blog (
id integer not null auto_increment,
title varchar(255),
content varchar(255),
createTime date,
primary key (id)
) type=InnoDB
Hibernate:
create table comment (
id integer not null auto_increment,
user varchar(255),
email varchar(255),
url varchar(255),
content varchar(255),
createTime date,
blogId integer not null,
primary key (id)
) type=InnoDB
Hibernate:
alter table comment
add index FK38A5EE5FDE0F1321 (blogId),
add constraint FK38A5EE5FDE0F1321
foreign key (blogId)
references blog (id)
而我认为正确的情况下。最后一节添加外键关系的SQL语句应该是这样的:
alter table comment
add index FK38A5EE5FDE0F1321 (blogId),
add constraint FK38A5EE5FDE0F1321
foreign key (blogId)
references blog (id)
on delete cascade
结果我在删除Blog的时候抛出如下异常:
Cannot delete or update a parent row: a foreign key constraint fails (`blog/comment`, CONSTRAINT `FK38A5EE5FDE0F1321` FOREIGN KEY (`blogId`) REFERENCES `blog` (`id`))
我程序的main方法具体如下:
public static void main(String[] args) {
Session session = HibernateUtil.currentSession();
Blog blog = new Blog();
blog.setTitle("a title");
session.save(blog);
Comment comment = new Comment();
comment.setContent("good");
comment.setBlog(blog);
session.save(comment);
session.delete(blog);
session.flush();
HibernateUtil.closeSession();
}
相关推荐
在数据库MySQL中利用外键实现级联删除、更新等方面的内容。有兴趣可以看看。。
LINQ通过外键增加删除数据库主从表的示例, 适合刚刚接触LINQ的人使用. 示例使用VS2008编写, 内置了数据库文件, 直接打开website就可以, 不支持VS2003和VS2005
Hibernate使用外键的一个简单直接的例子,便于学习者很好地理解 DB 的配置操作
专用于HIBERNATE中两个对象间的外键关联查询
主要介绍了MySQL使用外键实现级联删除与更新的方法,详细分析了mysql数据库与表的创建、数据插入、查询以及外链的使用与级联操作相关技巧,需要的朋友可以参考下
利用sql server自带的级联删除、级联更新功能,即其外键约束途径 alter table dbo.C add constraint FK_C_A_AID foreign key(AID) references dbo.A(AID) on delete cascade on update cascade go alter table dbo.C...
sql存在检测、建库、建表、约束、外键、级联删除[文].pdf
sql_存在检测、建库、_建表、约束、外键、级联删除[归类].pdf
很奇怪),分别是CascadeType.PERSIST(级联新建),CascadeType.REMOVE(级联删除),CascadeType.REFRESH(级联刷新),CascadeType.MERGE(级联更新),CascadeType.ALL(全部四项) * 方法一 * 主表: ?@OneToOne...
hibernate基于主外键的一对多/多对一关联
NULL 博文链接:https://cdxs2.iteye.com/blog/1932507
hibernate一对一外键关系,很适合入门者学习.
博文链接:https://shaqiang32.iteye.com/blog/201311
NULL 博文链接:https://paladin1988.iteye.com/blog/1627597
hibernate外键实现一对一双向关联关系源码
hibernate外键实现一对一单向关联关系源码
1、在有外键的一方,可以维护关联关系,可以建立关联关系,同样也可以解除关联关系,可以任意删除本对象,如果在hbm.xml中设置了cascade="delete",也可以删除关联对象 2、在没有外键的一方,不可以维护关联关系,...
Hibernate级联操作对象的关系属性: 映射文件中设置: 1. Cascade a) none b) Save-update 当使用session的api对当前对象进行save,update操作时,对它的关系属性也进行save或者update c) Delete d) All = delete ...
hibernate中基于外键的one2one,关键看配置文件是怎么配置的,深入浅出,很具有学习价值