-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: practice, how to connect the classes
PostPosted: Fri Sep 09, 2016 12:03 pm 
Newbie

Joined: Sun Sep 04, 2016 6:37 am
Posts: 6
I 'm trying to create this db using entity annotation:
Image


The reletions:
Model 0,N Article , Article 1,1 Model
User 0,N Cart , Cart 1,1 User
Article 0,N Cart, Cart 1,1 Article


I created the most part of the class and the main, but the part of build relationships stops me.
This is my situation:
Code:
@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();
      }
   }
}

how I can modify the code to have the previous scheme?
I also believe that my main is not perfect, and at run/compaile would give me some problems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.