熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

hibernate關聯關系-組件關聯

2022-06-13   來源: Java開源技術 

  模型User — Name(兩者同屬一張表中)

  Java代碼

  package Domain;

  import javautilDate;

  public class User {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public Name getName() {

  return name;

  }

  public void setName(Name name) {

  thisname = name;

  }

  public Date getBirthday() {

  return birthday;

  }

  public void setBirthday(Date birthday) {

  thisbirthday = birthday;

  }

  private int id;

  // private String name;

  private Name name;

  private Date birthday;

  }

  Java代碼

  package Domain;

  public class Name {

  public String getFirstName() {

  return firstName;

  }

  public void setFirstName(String firstName) {

  thisfirstName = firstName;

  }

  public String getLastName() {

  return lastName;

  }

  public void setLastName(String lastName) {

  thislastName = lastName;

  }

  private String firstName;

  private String lastName;

  }

  Java代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  mappingdtd>

  <hibernatemapping package=Domain>

  <class name=User table=user>

  <id name=id>

  <generator class=native />

  </id>

  <!

  <property name=name unique=true/>

  >

  <component name=name>

  <property name=firstName column=first_name/>

  <property name=lastName column=last_name/>

  </component>

  <property name=birthday/>

  </class>

  </hibernatemapping>

  Java代碼

  package DaoTest;

  import javautilDate;

  import DaoUserDAO;

  import DaoImplUserDAOImpl;

  import DomainName;

  import DomainUser;

  public class UserDAOTest {

  public static void main(String[] args) {

  /*UserDAO dao = new UserDAOImpl();

  User user = new User();

  usersetName(Eric);

  usersetBirthday(new Date());

  Systemoutprintln(==============================================);

  daosaveUser(user);

  Systemoutprintln(==============================================);

  usersetName(new Eric);

  daoupdateUser(user);

  Systemoutprintln(==============================================);

  User u = daofindUserByName(usergetName());

  Systemoutprintln(==============================================);

  daoremoveUser(u);*/

  User user = new User();

  usersetBirthday(new Date());

  Name n = new Name();

  nsetFirstName(firstName);

  nsetLastName(lastName);

  usersetName(n);

  UserDAO dao = new UserDAOImpl();

  daosaveUser(user);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27927.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.