Hibernate一对一主键关联(基于annotation注解方式)

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表中记录的主键一致。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值