hibernate 5.2.2:db translation logic

时间:2016-09-10 09:59:39

标签: java hibernate

我试图通过几个例子来学习db转换逻辑,并了解如何使用实体注释更改此db:DB example 我在逻辑问题中加入了1,N和N,N个版本,我在前面的模式中将其翻译成:

模型0,N条,第1,1条模型。

用户0,N购物车,购物车1,1用户。

第0条,N购物车,购物车1,1条。

我创建了大部分类和主要部分,但构建关系的一部分阻止了我。 这是我的情况:

型号:

@Entity   
@Table(name = "MODEL") 
public class Model{
   private int id;
   private double cost;
   private double price;

   public model(int id,double cost,double price){
         this.id=id;
         this.cost=cost;
         this.price=price;
   }

   @Id
   @Column(name="idModel")
   public void setId(int id){this.id=id;}
   public int getId(){return id;}

   @Column(name="cost")
   public void setCost(double cost){this.cost=cost;}
   public double getCost(){return cost;}

   @Column(name="price")
   public void setPrice(double price){this.price=price;}
   public double getPrice(){return price;}
}

文章:

@Entity   
@Table(name = "ARTICLE")
public class Article{
   private int id;
   private String code;
   private int quantity;
   private Model thisModel;

   public Article(int id, String code, int quantity, Model model){
      this.id=id;
      this.code=code;
      this.quantity=quantity;
      thisModel=model;
   }

   @Id
   @Column(name="idArticle")
   public void setId(int id){this.id=id;}
   public int getId(){return id;}

   @Column(name="code")
   public void setCode(String code){this.code=code;}
   public String getCode(){return code;}

   @Column(name="quantity")
   public void setQuantity(int quantity){this.quantity=quantity;}
   public int getQuantity(){return quantity;}

   @Column(name="model_idModel")
   public void setModel(Model model){this.model=model;}
   public Model getModel(){return model;}
}

用户:

@Entity   
@Table(name = "USER")
public class User{
   private int id
   private String name;

   public User(int id,String name){
      this.id=id;
      this.name=name;
   }

   @Id
   @Column(name="idUser")
   public void setId(int id){this.id=id;}
   public int getId(){return id;}

   @Column(name="name")
   public void seName(String name){this.name=name;}
   public int getName(){return name;}
}

购物车:

@Entity   
@Table(name = "CART")
public class Cart   {
   private User user;
   private Article article;

   publi Cart(User user,Article article){
      this.user=user;
      this.article=article;
   }

   @Id
   @Column(name="idUser")
   public void setUser(User user){this.user=user;}
   public User getUser(){return user;}

   @Id
   @Column(name="idArticle")
   public void setArticle(Article article){this.article=article;}
   public User getArticle(){return article;}
}

主:

public class test(){   
   public static void main(String[] args) {
      SessionFactory factory = new    Configuration().configure().buildSessionFactory();
      Session session=factory.getCurrentSession();
      Transaction transaction = null;
      try {
         transaction = session.beginTransaction();
         Model m1= new Model (1,12,24);
         Model m2= new Model (2,40,88);
         Article a1m1 = new Article(1,"blue",200,m1);
         Article a2m1 = new Article(2,"rad",100,m1);
         Article a1m2 = new Article(1,"only green",2000,m2);
         User u1 = new User(1,"Max");
         User u2 = new User(2,"Ada");
         Set<Cart> Carts = new HashSet<Cart>();
         carts.add(new Cart(u1,a2m1));
         carts.add(new Cart(u2,a1m2));
         session.save(a1m1);
         session.save(a2m1);
         session.save(a1m2);
         session.save(u1);
         session.save(u2);
         session.save(Carts);
         transaction.commit();
      } catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      } finally {
         session.close();
      }
   }
}

我也相信我的主要内容并不完美,而且在run / compaile会给我一些问题,而且我在这个db翻译中失去了一些逻辑概念。

1 个答案:

答案 0 :(得分:0)

关系的注释与非关系列的注释不同。对于关系列,请使用@ManyToOne@OneToMany代替@Column

不要问为什么,使用@Column让hibernate认为复杂类型应该映射列,而不是它的关系。