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

Hibernate利用XDoclet自動生成配置文件

2013-11-23 20:32:34  來源: Java開源技術 

  manytomany為例有Position和User兩張表一個Position可以有多個Users一個User也可以有多個 Position中間的關聯表為 test_user_position 通過在PO中加入XDoclet自動生成hbm配置文件不廢話看代碼

    

  package test;

  import javautilSet;
    import javautilTreeSet;

  /** *//**
     * @hibernateclass table=test_position
     */
    public class Position {

  private int id;

  private int name;

  private Set<Users> users = new TreeSet<Users>();

  /** *//**
         * @hibernateid generatorclass=identity type=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public int getName() {
            return name;
        }

  public void setName(int name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset inverse=true lazy=true table=test_user_position
          * llectionkey column=position_id
          * llectionmanytomany class=testUsers column=user_id
          */
        public Set<Users> getUsers() {
            return users;
        }

  public void setUsers(Set<Users> users) {
            thisusers = users;
        }
    }

    package test;

  import javautilSet;
    import javautilTreeSet;

  /** *//**
     * @hibernateclass <A title=table target=_blank>table</A>=test_position
     */
    public class Position {

  private int id;

  private int name;

  private Set<Users> users = new TreeSet<Users>();

  /** *//**
         * @hibernateid generatorclass=identity type=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public int getName() {
            return name;
        }

  public void setName(int name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset inverse=true lazy=true <A title=table target=_blank>table</A>=test_user_position
          * llectionkey column=position_id
          * llectionmanytomany class=testUsers column=user_id
          */
        public Set<Users> getUsers() {
            return users;
        }

  public void setUsers(Set<Users> users) {
            thisusers = users;
        }
    }

  package test;

  import javautilSet;
    import javautilTreeSet;

  /** *//**
     * @hibernateclass table=test_position
     */
    public class Position {

  private int id;

          private int name;

  private Set<Users> users = new TreeSet<Users>();

  /** *//**
         * @hibernateid generatorclass=identity type=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public int getName() {
            return name;
        }

  public void setName(int name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset inverse=true lazy=true table=test_user_position
          * llectionkey column=position_id
          * llectionmanytomany class=testUsers column=user_id
          */
        public Set<Users> getUsers() {
            return users;
        }

  public void setUsers(Set<Users> users) {
            thisusers = users;
        }
    }

  接下來是Usersjava

  Java代碼
    package test;

  import javautil*;

  /** *//**
     * @hibernateclass table=test_uses
     */
    public class Users {

  private int id;

  private String name;

  private Set<Position> positions = new TreeSet<Position>();

  /** *//**
         * @hibernateid generatorclass=identity typ=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public String getName() {
            return name;
        }

  public void setName(String name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset table=test_user_position lazy=true
          * llectionkey column=user_id
          * llectionmanytomany class=testPosition column=position_id
          */
        public Set<Position> getPositions() {
            return positions;
        }

  public void setPositions(Set<Position> positions) {
            thispositions = positions;
        }
    }
    view plaincopy to clipboardprint?
    package test;

  import javautil*;

  /** *//**
     * @hibernateclass <A title=table target=_blank>table</A>=test_uses
     */
    public class Users {

  private int id;

  private String name;

  private Set<Position> positions = new TreeSet<Position>();

  /** *//**

           * @hibernateid generatorclass=identity typ=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public String getName() {
            return name;
        }

  public void setName(String name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset <A title=table target=_blank>table</A>=test_user_position lazy=true
          * llectionkey column=user_id
          * llectionmanytomany class=testPosition column=position_id
          */
        public Set<Position> getPositions() {
            return positions;
        }

  public void setPositions(Set<Position> positions) {
            thispositions = positions;
        }
    }

  package test;

  import javautil*;

  /** *//**
     * @hibernateclass table=test_uses
     */
    public class Users {

  private int id;

  private String name;

  private Set<Position> positions = new TreeSet<Position>();

  /** *//**
         * @hibernateid generatorclass=identity typ=int
         */
        public int getId() {
            return id;
        }

  public void setId(int id) {
            thisid = id;
        }

  /** *//**
         * @hibernateproperty length=
         */
        public String getName() {
            return name;
        }

  public void setName(String name) {
            thisname = name;
        }

  /** *//**
          * @hibernateset table=test_user_position lazy=true
          * llectionkey column=user_id
          * llectionmanytomany class=testPosition column=position_id
          */
        public Set<Position> getPositions() {
            return positions;
        }

  public void setPositions(Set<Position> positions) {
            thispositions = positions;
        }
    }

    在Myeclipse中右鍵點擊項目選擇Properties從界面中選擇Myeclipse/XDoclet點擊Add Standard添加Standard Hibernate點擊OK結束設置      在項目中建立Hibernatecfgxml配置好SessionFactory和數據源      右鍵點擊項目選擇Myeclipse/Run XDoclet將自動創建以上兩個類對應的hbm文件      注意創建完成的hbm文件存在問題裡面有role和readonly屬性將前者改為name後者刪掉即可


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28643.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.