- package com.javamodel.hibernate;
-
- Person.java
代码- package com.javamodel.hibernate;
- public class Person {
- private String id = null;
- private String name = null;
- private String email = null;
- public Person(){}
- /**
- * @return
- */
- public String getEmail() {
- return email;
- }
- /**
- * @return
- */
- public String getId() {
- return id;
- }
- /**
- * @return
- */
- public String getName() {
- return name;
- }
- /**
- * @param string
- */
- public void setEmail(String string) {
- email = string;
- }
- /**
- * @param string
- */
- public void setId(String string) {
- id = string;
- }
- /**
- * @param string
- */
- public void setName(String string) {
- name = string;
- }
- }
- public class Author{
- private String id ;
- private String alias = null;
- private Person person = null;
- /**
- * @return
- */
- public String getAlias() {
- return alias;
- }
- /**
- * @return
- */
- public Person getPerson() {
- return person;
- }
- /**
- * @param string
- */
- public void setAlias(String string) {
- alias = string;
- }
- /**
- * @param person
- */
- public void setPerson(Person person) {
- this.person = person;
- }
- /**
- * @return
- */
- public String getId() {
- return id;
- }
- /**
- * @param i
- */
- public void setId(String i) {
- id = i;
- }
- }
author.hbm.xml
代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
- <hibernate-mapping>
- <class name="com.javamodel.hibernate.Author" table="author" >
- <id name="id" column="id">
- <generator class="foreign">
- <param name="property">person</param>
- </generator>
- </id>
- <property name="alias" column="alias" />
- <one-to-one name="person" class="com.javamodel.hibernate.Person" cascade="all" constrained="true" />
- </class>
- </hibernate-mapping>
Example.java
代码
- package com.javamodel.hibernate;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import net.sf.hibernate.HibernateException;
- import net.sf.hibernate.MappingException;
- import net.sf.hibernate.Session;
- import net.sf.hibernate.SessionFactory;
- import net.sf.hibernate.Transaction;
- import net.sf.hibernate.cfg.Configuration;
- public class Example{
- private static SessionFactory _sessions = null;
- private static Properties pops = new Properties();
- static{
- try {
- InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
- try {
- pops.load(stream);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Configuration cfg = new Configuration();
- cfg.addClass(Person.class);
- cfg.addClass(Author.class);
- cfg.setProperties(pops);
- _sessions = cfg.buildSessionFactory();
- } catch (MappingException e) {
- e.printStackTrace();
- } catch (HibernateException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws HibernateException {
- Person person = new Person();
- person.setName("HengfeiDo");
- person.setEmail("smallduzi@sohu.com");
- Author author = new Author();
- author.setAlias("smallduzi");
- author.setPerson(person);
- Session session = _sessions.openSession();
- Transaction tx = null;
- try{
- tx = session.beginTransaction();
- session.save(author);
- tx.commit();
- System.out.println("over");
- }catch(HibernateException he){
- if(tx != null) tx.rollback();
- throw he;
- }
- finally{
- session.close();
- }
- }
- }