Im learning JPA and I have an entity class named User that has two lists of another entity, Car, but i cant get it to work with jpa.
The classes have all the getters, setters and constructors needed.
This is my User class:
#Entity
#Table(name = "Users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(unique = true)
private String userName;
#OneToMany(i dont know what goes here as well)
private List<Car> owned;
#OneToMany( ?? )
private List<Car> rented;
This is my Car class:
#Entity
#Table(name = "Cars")
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne ( ?? )
private User owner;
My two questions would be:
What annotations go to each #Something to make this work?
After i have all set up, how would i add a new car to an existing user and persist it as well?
Thank you!
Related
Given the following entity classes, examine the possible way to
Create bidirectional One (Student) to One (Account) relationship
Create unidirectional One (Student) to Many (Account) relationship
Here down is Student entity:
#Entity
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int password;
public Student() {
}
}
Here down is Account entity:
#Entity
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private double balance;
public Account() {
}
}
I have two different tables, Pizza and User.
First of all, when I create the first entity from for example the user, ID is 1. After that, when I want to create a Pizza, the ID of it will be 2. They sharing the ID somehow. How can I fix that?
And my other question, I have a third table, named Orders. I managed to ask on Order creating for the valid User and Pizza ID, but I just wanna know, what would be the best solution for it?
I just created the Pizza and User Service object in the Order Controller as well, and refered from there. But I don't know how bad is it.
Thank you for the answers!
#Entity(name = "pizza")
#Table(name = "pizza")
public class Pizza {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
private String type;
#Entity(name = "users")
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
private String email;
#NotNull
private String address;
#Entity(name = "orders")
#Table(name = "orders")
public class Order {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#NotNull
private int user_id;
#NotNull
private int pizza_id;
#RestController
public class OrderController {
#Autowired
private OrderService orderService;
#Autowired
private UserService userService;
#Autowired
private PizzaService pizzaService;
try to use identity generation type instead of auto:
#GeneratedValue(strategy = GenerationType.IDENTITY)
for your first question I think you should change :
#GeneratedValue(strategy = GenerationType.AUTO)
to
#GeneratedValue(strategy = GenerationType.IDENTITY)
for your second question I think that you have to use relationship annotation between your entities :
like:
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ID_USER")
Private User user;
I have the class SWEntity that is central to the problem. I want set of classes SWEntityRow that is a detail of the SWEntity (one entity has many rows). The SWentity has in the key two other classes in the embeddedId Area and Procedure. When I try to map SWEntity with SWEntityRow using SWEntityRowId I take differents errors.
Some idea how I can map SWEntityRowId with SWEntity?
This is a simplified picture of the ER: https://i.stack.imgur.com/pMJzh.png
And this are my simplified classes:
SWentity
-----------
#Entity
public class SWEntity {
#EmbeddedId
private SWEntityId id;
[...]
}
SWEntityId
-------------
#Embeddable
public class SWEntityId implements Serializable{
private static final long serialVersionUID = 1L;
#NotNull
private String name;
#NotNull
private int version;
#ManyToOne
#JoinColumn(name = "areaName", nullable = false)
#JsonIgnore
private Area area;
#ManyToOne
#JoinColumn(name = "procedureName", nullable = false)
#JsonIgnore
private Procedure procedure;
}
SWEntityRow
---------------
#Entity
public class SWEntityRow{
#EmbeddedId
private SWEntityRowId sWEntityRowId;
}
SWEntityRowId
---------------
#Embeddable
public class SWEntityRowId implements Serializable {
private static final long serialVersionUID = 1L;
private String rowName;
//SWEntityId
#ManyToOne
#JoinColumns({
#JoinColumn(name="name_row", referencedColumnName="name"),
#JoinColumn(name="version_row", referencedColumnName="version"),
#JoinColumn(name="area_row", referencedColumnName="area_name"),
#JoinColumn(name="procedure_row", referencedColumnName="procedure_name"
})
}
This resolve my question
# JoinColumns({
#JoinColumn(name="entityname", referencedColumnName="name"),
#JoinColumn(name="entityversion", referencedColumnName="version"),
#JoinColumn(name="entityarea", referencedColumnName="AreaName"),
#JoinColumn(name="entityprocedure", referencedColumnName="ProcedureName")
})
The difference is in referencedColumnName="AreaName" not referencedColumnName="area_name" and the same for procedure.
Thanks!
Just one change required
#Entity
public class SWEntity {
#Id
#EmbeddedId
private SWEntityId id;
[...]
}
i have an Entity with following mappings:
#Entity
#Table(name = "template_product")
public class TemplateProductBean implements Serializable {
private static final long serialVersionUID = -1821696115330320798L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Column(name = "product_id")
private Long productId;
// bi-directional many-to-one association to
// Template
#ManyToOne
#JoinColumn(name = "template_id")
private TemplateBean template;
The Template Bean looks as following:
#Entity
#Table(name = "template")
public class TemplateBean implements Serializable {
private static final long serialVersionUID = 3752018564161042623L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "platform_id")
private Long platformId;
#Column(name = "template_name")
private String templateName;
// bi-directional many-to-one association to
// TemplateProduct
#OneToMany(mappedBy = "template")
private List<TemplateProductBean > templateProductBean;
The Problem im facing is, im very untrained when it comes down to use the JPA Interfaces. How can i use these to get the following query:
select template
from Template template
join TemplateProductBean templateProducts
on template.templateId = templateProducts.template.templateId
where templateProducts.productId in :templateProductIdList
How can i use the JPA Interfaces( javax.persistence.criteria.Root, javax.persistence.Join etc.) to build this query as a Predicate? I'm sorry if im being unclear, i have to use this and im not used to using the JPA. Thanks
I think this should work
select t from TemplateBean t inner join t.templateProductBean tpb where tpb.productId in :templateProductIdList
More details on inner joins and HQL in general: documentation
I am currently developing a java ee application but i am having problems with the JPA.
I have two entities:
#Entitiy
public class Restaurant implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Desk> desks;
}
#Entitiy
public class Desk implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
#JoinColumn(name = "restaurant_id")
private Restaurant restaurant;
}
And i am storing the desks with the following code:
Desk desk = new Desk();
desk.setNumber(Integer.toString(x));
desk.setRestaurant(restaurant);
em.persist(desk);
But now the strange thing is that the list of desks in the entity restaurant is empty but the restaurant value in the entity desk is correct.
The Database shema looks like that:
RESTAURANT (ID)
DESK (ID, RESTAURANT_ID)
RESTAURANT_DESK (RESTAURANT_ID, DESK_ID)
The table RESTAURANT_DESK is always empty. Why is this third table generated? And why is the list of desks in the entity restaurant empty?
For a bi-directional mapping, you need to give mappedBy attribute in the Restaurant entity to indicate the inverse relationship:
#Entity
public class Restaurant implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="restaurant")
private List<Desk> desks;
}
I found the problem. I had to add the entity desk also to the restaurant list.
Desk desk = new Desk();
desk.setNumber(Integer.toString(x));
desk.setRestaurant(restaurant);
restaurant.getDesks().add(desk);
em.persist(desk);