hibernate基于注解的常规用法

这篇文章主要是配合

http://finallygo.iteye.com/blog/1040373的文章写的

 

1.单表的情况,比如新闻

//注意,这里的Entity是javax.persistence.Entity,不是hibernate中的

 

@Entity
public class News{
        @Id
        @GeneratedValue
        private Integer id;//新闻id
        private String newsTitle;//新闻标题
        private String newsContent;//新闻内容
        private Date updateDt;//新闻更新时间
        ...省略get,set方法
}
 

 

2.多对一的情况,比如要给新闻加一个新闻类型NewsType,

 

 

@Entity
public class News{
        @Id
        @GeneratedValue
        private Integer id;//新闻id
        private String newsTitle;//新闻标题
        private String newsContent;//新闻内容
        @ManyToOne
        private NewsType newsType;//NewsType的配置参考单表的配置
        private Date updateDt;//新闻更新时间
        ...省略get,set方法
}
 

3.一对多的情况,比如现在要给新闻类型添加该新闻类型的新闻集合

 

@Entity
public class NewsType{
        @Id
        @GeneratedValue
        private Integer id;//新闻类型id
        private String typeName;//新闻类型名
        @OneToMany(mappedBy="newsType")//这里的值对应News类中的相应属性
        private Set<News> newsSet;//该新闻类型的新闻集合
}
 

4.多对多的情况,将多对多转换为两个一对多,中间表建立对应实体,具体结合一对多和多对一的配置

后来发现已经有人写了类似的文章了,请参考

英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-entity

中文:http://chicuping.blog.163.com/blog/static/433097832009111991826632/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值