hibernate中一对一的关联有两种方式:一种是采用外键关联,另外一种是采用主键关联。
项目中用到Hibernate(annotation注解方式)构建实体类,其中有一对一主键双向关联。期间遇到一些问题,现在贴出来探讨探讨。 一个帖子内容类(PostText)对应一个帖子信息类(Post),主要目标是在存储帖子或者帖子内容时,关联的对象也被存储。具体代码如下:
Post类
@Entity
@Table(name = "posts")
public class Post implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成器
@Column(name="post_id",unique=true)
private int id;
@Column(name ="topic_id",nullable=false)
private int topicId;
@Column(name ="forum_id",nullable=false)
private int forumId;
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn//这个注解只能写在主(生成ID)的一端
private PostText postText;
/*相应的get和set方法。。。*/
PostText类
@Entity
@Table(name = "jforum_posts_text")
public class PostText implements Serializable {
@Id
@GenericGenerator(name ="pkGenerator",strategy="foreign" ,parameters={@Parameter(name="property",value="post")})
@GeneratedValue(generator="pkGenerator")
//post_text的ID是根据post的ID来赋值的,这里需要设置ID生成器的策略为foreign,参数中指定post_text的ID是使用post对象中的ID
private int id;
@Column(name ="post_text",nullable=true)
private String text;
@Column(name ="post_subject",nullable=true)
private String subject;
@OneToOne(cascade=CascadeType.ALL, mappedBy="postText") // 一对一
private Post post;
/*相应的get和set方法。。。*/
写个OneToOnePKTest类:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.edu.dlnu.resources.model.entity.Post;
import cn.edu.dlnu.resources.model.entity.PostText;
public class OneToOnePKTest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sessionFactory.close();
}
@Test
public void testSave(){
Session s = sessionFactory.getCurrentSession();
s.beginTransaction();
Post post = new Post();
PostText postText = new PostText();
post.setForumId(1);
postText.setText("test");
post.setPostText(postText);
postText.setPost(post);
s.save(postText);
s.getTransaction().commit();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
public static void main(String[] args) {
beforeClass();
new OneToOnePKTest().testSave();
afterClass();
}
}
运行testSave(),方法后数据库中post 与 post_text表中记录的主键一致。
分享到:
相关推荐
NULL 博文链接:https://paladin1988.iteye.com/blog/1639102
使用@OneToOne注解建立实体Bean之间的一对一关联。一对一关联有三种情况:(1).关联的实体都共享同样的主键,(2).其中一个实体通过外键关联到另一个实体的主键(注意要模拟一对一关联必须在外键列上添加唯一约束),(3)...
前言 1. 翻译说明 ...4.2.2. Hibernate基于事件的验证 4.2.3. 程序级验证 4.2.4. 验证信息 5. Hibernate与Lucene集成 5.1. 使用Lucene为实体建立索引 5.1.1. 注解领域模型 5.1.2. 启用自动索引 A. 术语表
Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射
2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. 访问类型 2.4.3.2. 公式 2.4.3.3. 类型 2.4.3.4. 索引 2.4.3.5. @Parent 2.4.3.6. 生成的属性 2.4.4. 继承 2.4.5. 关于...
Hibernate 可以对类的属性或者方法进行注解。属性对应field类别,方法的 getXxx()对应property类别。 定义表 通过 @Table 为实体Bean指定对应数据库表,目录和schema的名字。 @Entity @Table(name="tbl_sky") ...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...
使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...
使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...
5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @Resource 3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管...
3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @Resource 3.11.5. @PostConstruct 与 @PreDestroy 3.12. 对受管...
通过网络或磁盘等方式,把公钥编码传送给李四,李四接收到张三编码后的公钥,将其解码,李四用张三的公钥加密信息,并发送给李四,张三用自己的私钥解密从李四处收到的信息…… Java利用DES私钥对称加密代码实例 ...