联合主键--Annotation
联合主键Annotation配置方法有三种:
1.将组件类注解为@EmbeddedId,并将组建的属性注解为@Id
2.将组件的属性注解为@EmbeddeId
3.将类注解为@IdClass,并将该实体中所有的主键的属性都注解为@Id
?
第一种:
1.定义注解类并其直接为@Embeddable
package com.zchen.hibernate.sxt.test;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import com.zchen.hibernate.sxt.domain.StudentPK;import com.zchen.hibernate.sxt.domain.Students;public class StudentsTest {private static SessionFactory sf = null;@BeforeClasspublic static void beforeClass(){try {sf = new AnnotationConfiguration().configure().buildSessionFactory();} catch (HibernateException e) {e.printStackTrace();}}@Testpublic void addStduentTest(){/*StudentPK pk = new StudentPK();pk.setId(3);pk.setName("aaa");*/Students s = new Students();s.setId(4);s.setName("pig");s.setAge(20);s.setAddress("湖南");//s.setPk(pk);Session session = sf.getCurrentSession();session.beginTransaction();session.save(s);session.getTransaction().commit();}@AfterClasspublic static void afterClass(){if(sf != null){try {sf.close();} catch (HibernateException e) {e.printStackTrace();}}}}
?
?
?