I 'm trying to create this db using entity annotation:
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