java培训7

来源:互联网 发布:北京搜索引擎优化 编辑:程序博客网 时间:2024/05/16 07:06

1.       HIBERNATE的实体类设计与表的创建

根据(二、2)的题目,画出相应的UML类图

完成

建立HIBERNATE的MODEL层,并通过MODEL层创建出相关的数据表

 

hibernate.Cfg.xml添加语句<propertyname=”hibernate.hbm2ddl.auto”>create</property>

 

使用HIBERNATE ANNOTATION的方式

   packagehibernate.model;

 

import java.util.*;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;

import javax.persistence.OneToMany;

import javax.persistence.OneToOne;

import javax.persistence.PrimaryKeyJoinColumn;

import javax.persistence.Table;

 

import org.hibernate.annotations.*;

 

 

@Entity

@Table(name="bag")

public class Bag {

        

        

        @Id

        @GeneratedValue(generator="mysqlIncrement")

        @GenericGenerator(name="mysqlIncrement",strategy="increment")

        private int id;

        

        

        @Column(name="capacity")

        private int capacity;

        

        

        @OneToOne()

        @PrimaryKeyJoinColumn

        private Player hostId;

        

        

        @OneToMany(mappedBy="bagId",cascade=CascadeType.ALL)

        private Set<Drug> drug=newHashSet<Drug>();

        

        

        @OneToMany(mappedBy="bagId",cascade=CascadeType.ALL)

        private Set<Equipment> equipment =newHashSet<Equipment>();

        

        

        @OneToMany(mappedBy="bagId",cascade=CascadeType.ALL)

        private Set<Ball> ball=newHashSet<Ball>();

 

        

 

}

实现基本的CRUD操作,并实现相应的测试用例。

package hibernate;

import java.util.HashSet;

import java.util.Set;

 

import hibernate.model.*;

 

import org.hibernate.*;

import org.hibernate.cfg.AnnotationConfiguration;

 

 

 

public class Test {

        

        

        AnnotationConfiguration cfg;

        

   

   SessionFactory sf;

   

   

    Sessionsession;

   

   

    voiddoConfiguration(){

           

            cfg=newAnnotationConfiguration();       

            cfg.configure("/hibernate/hibernate.cfg.xml");

        sf=cfg.buildSessionFactory();

           

    }

   

   

    voidopenSession(){

           try{

                     session=sf.openSession();

           }catch(Exception e){

                     

           }

    }

   

   

    voidcloseSession(){

           try{

                     session.close();

           }catch(Exception e){

                     

           }

    }

   

   

    voidsaveEntity(){

           

           Transaction tx1=session.beginTransaction();

           

           

           Player player=new Player();

   

           player.setName("test");

           player.setGrade(1);

           player.setExp(10);

           player.setJob("test");

           

           

           Bag bag=new Bag();

           bag.setCapacity(100);

           

           

           

           Pet pet=new Pet();

           pet.setExp(10);

           pet.setGrade(1);

           pet.setHostId(player);

           pet.setName("pet");

           

           

           Equipment equipment=new Equipment();

           equipment.setAbility(140);

           equipment.setAmount(20);

           equipment.setBagId(bag);

           

           

           Ball ball=new Ball();

           ball.setBagId(bag);

           ball.setPetId(pet);

           ball.setAmount(30);

   

           

           

           Drug drug=new Drug();

           drug.setAmount(30);

           drug.setBagId(bag);

           

           Set<Pet>  set=new HashSet<Pet>();

           set.add(pet);

           player.setPet(set);

           

           

           player.setBag(bag);

           

           Set<Ball>  set2=new HashSet<Ball>();

           set2.add(ball);

           pet.setBall(set2);

           

           Set<Drug>  set3=new HashSet<Drug>();

           set3.add(drug);

           bag.setDrug(set3);

           

           Set<Ball>  set4=new HashSet<Ball>();

           set4.add(ball);

           bag.setBall(set4);

           

           Set<Equipment>  set5=new HashSet<Equipment>();

           set5.add(equipment);

           bag.setEquipment(set5);

           

           session.save(player);

           

           

           

           tx1.commit();

           

           

    }

   

   

    voiddeleteEntity(){

           

           Transaction tx3=session.beginTransaction();

   

                     Player player=(Player)session.load(Player.class, newInteger(1));

                     session.delete(player);

           

           tx3.commit();

    }

   

   

    publicstatic void main(String args[]){

   

           Test test=new Test();

   

           test.doConfiguration();

           test.openSession();

           test.saveEntity();

           //System.out.print("sadfsaf");

           //test.updateEntity();

           

           //test.queryEntity();

           test.deleteEntity();

           test.closeSession();

           

    }

}

要求:保存玩家数据时,自动把相应的宠物、背包、物品保存到数据库

 

按要求全部完成