您的位置:首页 > 其它

<many to one>和<one to many>

2013-12-22 17:33 369 查看
一对多,多对一(双向关联)

1、PO

//User类

public class User {

private int userId;

private String userName;

//在一的一方要引用多的一方的类,并把它放入到Set集合中

private Set<Video> videos = new HashSet<Video>();

public User() {

super();

}

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

}

//Video类

public class Video {

private int videoId;

private String title;

//在多的一方要引用一的一方

private User user;

public Video() {

super();

}

public int getVideoId() {

return videoId;

}

public void setVideoId(int videoId) {

this.videoId = videoId;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

}

2、配置文件

//User的配置文件

<hibernate-mapping

package="com.puckasoft.po">

<class name="User" table="user">

<id name="userId" column="user_id">

<generator class="native"></generator>

</id>

<property name="userName" column="user_name"></property>

<!-- 在一的一方要设置set,和PO里的Set集合相对应,外键就是一方的id -->

<set name="videos" cascade="save-update" inverse="true">

<key column="user_id"></key>

<one-to-many class="Video" />

</set>

</class>

</hibernate-mapping>

//Video的配置文件

<hibernate-mapping

package="com.puckasoft.po">

<class name="Video" table="video">

<id name="videoId" column="video_id">

<generator class="native"></generator>

</id>

<property name="title" column="title"></property>

<!-- 多的一方设置<many-to-one> -->

<many-to-one name="user" column="user_id"></many-to-one>

</class>

</hibernate-mapping>

3、配置hibernate.cfg.xml文件

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver

</property>

<property name="hibernate.connection.url">

jdbc:mysql://127.0.0.1:3306/hibernate

</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.format_sql">true</property>

<mapping resource="com/puckasoft/po/User.hbm.xml" />

<mapping resource="com/puckasoft/po/Video.hbm.xml" />

</session-factory>

</hibernate-configuration>

4、HibernateUtil类

public class HibernateUtil {

private static SessionFactory factory;

static{

try{

factory = new Configuration().configure().buildSessionFactory();

} catch(HibernateException e){

e.printStackTrace();

System.out.println("sessionFactory 创建失败");

throw new ExceptionInInitializerError();

}

}

public static SessionFactory getSessionFactory() {

return factory;

}

public static void closeSessionFactory() {

if(factory!=null){

factory.close();

}

}

public static Session getSession(){

return factory.openSession();

}

public static void closeSession(Session session){

if(session!=null){

session.close();

}

}

}

5、导表

public class ExportTable extends TestCase {

Configuration configuration;

protected void setUp() throws Exception {

System.out.println("invoke setUp");

configuration = new Configuration().configure();

}

protected void tearDown() throws Exception {

System.out.println("invoke tearDown");

}

public void testExport() throws Exception {

// new Configuration() 加载 classpath 下的 hibernate.properties 文件

// 加载 classpath 下的 hibernate.cfg.xml 文件

Configuration configuration = new Configuration().configure();

SchemaExport export = new SchemaExport(configuration);

export.create(true, true);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: