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();}@AfterClasspublic static void afterClass() {sessionFactory.close();} @Testpublic 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();}@Testpublic 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表中记录的主键一致。
?
1 楼 lsw289998792 2012-05-09 您好,按照您上面的代码我做了一次,我用的hibernate3.6.0,报Caused by: org.hibernate.id.IdentifierGenerationException: null id generated for:求指教,谢谢,