Help designin products model spring boot

I'm not really sure how to do it the right way I have products that have different sizes and colors and designs applied to them
35 Replies
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Suika
Suika2w ago
public class ProductFormat {
public Product product;
public Format format;
}
public class ProductFormat {
public Product product;
public Format format;
}
This message has been formatted automatically. You can disable this using /preferences.
userexit
userexitOP2w ago
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_generator")
@SequenceGenerator(name = "product_generator", sequenceName = "pdc_pk", allocationSize = 1, initialValue = 1)
private int id;
@ManyToOne
private Category category;
@ManyToOne
private Color color;
@ManyToMany
private Size size;
@ManyToMany
private List<Design> designs;
private double price;
private int stock;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_generator")
@SequenceGenerator(name = "product_generator", sequenceName = "pdc_pk", allocationSize = 1, initialValue = 1)
private int id;
@ManyToOne
private Category category;
@ManyToOne
private Color color;
@ManyToMany
private Size size;
@ManyToMany
private List<Design> designs;
private double price;
private int stock;
}
this is what I currently have @Peter Rader
Peter Rader
Peter Rader2w ago
Oh you mean the db-model.
userexit
userexitOP2w ago
yeah Im just not sure how to do it a product can have multiple colors multiple sizes different stock for each color, size combination say I have a tshirt with a goku design on it, in color red, with size XL, XS, M and then a tshirt with a goku design on it, in color black, with size XL, M, S, L, XXL stuff like this
Peter Rader
Peter Rader2w ago
Show the entity "design"
userexit
userexitOP2w ago
@Entity
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class Design {
@Id
@GeneratedValue(generator = "design_gen", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(
name = "design_gen",
sequenceName = "design_seq",
allocationSize = 1,
initialValue = 4
)
private int id;
@NonNull
@NotBlank(message = "The dessign name cannot be empty")
@Column(nullable = false)
private String name;
@ManyToMany
@JoinTable(
name = "design_character",
joinColumns = @JoinColumn(name = "design_id"),
inverseJoinColumns = @JoinColumn(name = "character_name")
)
@NonNull
@ToString.Exclude
private List<Character> characters;
private double price;
@ManyToMany(mappedBy = "designs")
@ToString.Exclude
private List<Product> products;
}
@Entity
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class Design {
@Id
@GeneratedValue(generator = "design_gen", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(
name = "design_gen",
sequenceName = "design_seq",
allocationSize = 1,
initialValue = 4
)
private int id;
@NonNull
@NotBlank(message = "The dessign name cannot be empty")
@Column(nullable = false)
private String name;
@ManyToMany
@JoinTable(
name = "design_character",
joinColumns = @JoinColumn(name = "design_id"),
inverseJoinColumns = @JoinColumn(name = "character_name")
)
@NonNull
@ToString.Exclude
private List<Character> characters;
private double price;
@ManyToMany(mappedBy = "designs")
@ToString.Exclude
private List<Product> products;
}
Peter Rader
Peter Rader2w ago
First, rename the Product into ProductVariant. Then make the field designs a one2many. Create a entity Product having a many2one to ProductVariant
userexit
userexitOP2w ago
why would it be a one2many, the same design for example "Goku vs Gohan" can be used in a tshirt a sweater etc
Peter Rader
Peter Rader2w ago
A tshirt is a different variant than a sweater Remove the price from the design
userexit
userexitOP2w ago
it still doesnt click in my mind ill have to draw it by hand brb no thats actually ok some designs are harder to embroid than others so i want prices for designs then prices for products like a tshirt is going to be x amount then the total price is the price of the tshirt for example + the price of the design
Peter Rader
Peter Rader2w ago
Then increase the price of the productvariant. Or dont call it price, call it "license_fee"
userexit
userexitOP2w ago
ok @Peter Rader Would you use this as a product entity:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_generator")
@SequenceGenerator(name = "product_generator", sequenceName = "product_seq", allocationSize = 1, initialValue = 1)
private int id;

private String name;
private String description;

@OneToMany(mappedBy = "product")
private List<ProductVariant> variants;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_generator")
@SequenceGenerator(name = "product_generator", sequenceName = "product_seq", allocationSize = 1, initialValue = 1)
private int id;

private String name;
private String description;

@OneToMany(mappedBy = "product")
private List<ProductVariant> variants;
}
and this as product variant:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductVariant {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_variant_generator")
@SequenceGenerator(name = "product_variant_generator", sequenceName = "variant_seq", allocationSize = 1, initialValue = 1)
private int id;

@ManyToOne
private Product product;

@ManyToOne
private Category category;

@ManyToOne
private Color color;

@ManyToMany
private Size size;

@ManyToMany
private List<Design> designs;

private double price;
private int stock;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductVariant {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_variant_generator")
@SequenceGenerator(name = "product_variant_generator", sequenceName = "variant_seq", allocationSize = 1, initialValue = 1)
private int id;

@ManyToOne
private Product product;

@ManyToOne
private Category category;

@ManyToOne
private Color color;

@ManyToMany
private Size size;

@ManyToMany
private List<Design> designs;

private double price;
private int stock;
}
Suika
Suika2w ago
@startuml
map Product {
1 => id
2 => name
3 => description
}
map ProductVariant {
1 => id
2 => product_id
3 => design_id
4 => price
5 => stock
6 => color
7 => size_id
}
map Design {
1 => id
2 => name
3 => license_fee
}
map Size {
1 => id
2 => size_name (xxl, xl, l, s etc.)
}
map Character {
1 => id
2 => character_name
}

map Design2Character {
1 => design_id
2 => character_id
}
map Product {
1 => id
2 => name
3 => description
}
map ProductVariant {
1 => id
2 => product_id
3 => design_id
4 => price
5 => stock
6 => color
7 => size_id
}
map Design {
1 => id
2 => name
3 => license_fee
}
map Size {
1 => id
2 => size_name (xxl, xl, l, s etc.)
}
map Character {
1 => id
2 => character_name
}

map Design2Character {
1 => design_id
2 => character_id
}
ProductVariant::product_id --> Product::id ProductVariant::design_id --> Design::id ProductVariant::size_id --> Size::id Design2Character::design_id --> Design::id Design2Character::character_id --> Character::id @enduml
This message has been formatted automatically. You can disable this using /preferences.
Peter Rader
Peter Rader2w ago
No description
userexit
userexitOP2w ago
productvariant and design are connected by a many to many ?
Peter Rader
Peter Rader2w ago
No the only many2many is design2character because a different design is naturally a different product-variant.
userexit
userexitOP2w ago
where do you put the category of the product, in Product or in ProductVariant as in t-shirt, sweater etc
Peter Rader
Peter Rader2w ago
Hm, I forgot about it.
userexit
userexitOP2w ago
I would put it in Product and you can have the same design in a tshirt and in a sweater, thats why i was suggesting the many2many with design
Peter Rader
Peter Rader2w ago
Depends on what you see as a category. If the Category "Wearable" should stick to the product. The category "Winter Closets" should stick to the ProductVariant because you wont wear a tanktop in winter, right?
userexit
userexitOP2w ago
category is just going to be T-shirt, sweater, crew neck stuff like that
Peter Rader
Peter Rader2w ago
Then it must stick to the productvariant.
Peter Rader
Peter Rader2w ago
No description
userexit
userexitOP2w ago
alright and now it is a many2many design and productvariant right since I can have a tshirt with same design as a hoodie or a crew neck 2 different product variants same design
Peter Rader
Peter Rader2w ago
No You have a Sweater with 2 different designs? Can you give an example? 50cent and HelloKitty on one sweater? You are kidding!
userexit
userexitOP2w ago
hahahah obv not 50cent and hellokitty but luffy and ace so the Product thing is useless if I dont need name and description
Peter Rader
Peter Rader2w ago
Know neither luffy nor ace. Are they rappers?
userexit
userexitOP2w ago
nah anime characters that are both blood related say u put Eminem on the front and Dr dre on the back if u want the rapper version of it
ayylmao123xdd
ayylmao123xdd2w ago
hmmmmmmmmm dont you think that could just be a string that way you dont have to restart the app every time you wanna add a new category same for the color
userexit
userexitOP2w ago
wdym with this
ayylmao123xdd
ayylmao123xdd2w ago
oh i thought you use an enum but yea is there any need to make the category a separate entity when you can just give it a string
userexit
userexitOP2w ago
yeah so they can shop by category ig
ayylmao123xdd
ayylmao123xdd2w ago
yea that makes sense then
JavaBot
JavaBot7d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?