JPA2: how to persist data using #MapKeyColumn - java

In my example I have two entities to persist. But when I try to using #MapKeyColumn annotation , I am confused on how to use this annotation and how to persist the data. This is the example that is explained in the "PRO JPA2 book". When I try to run this code it will generate the exception.
two entities:
#Entity
#Access(AccessType.FIELD)
public class Employee {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String salary;
#Enumerated(EnumType.STRING)
private EmployeeType type;
#ManyToOne
#JoinColumn(name="dept_id")
private Department deparment;
and
#Entity
public class Department {
#Id
#Column(name="dept_id")
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String Name;
#OneToMany(mappedBy="deparment")
#MapKeyColumn(name="cabin_id")
private Map<String , Employee> empCabin;
......................................
public Map<String, Employee> getEmpCabin() {
return empCabin;
}
public void assignEmpToCabin(Employee emp , String cabinNo) {
System.out.println(emp+" "+cabinNo);
getEmpCabin().put(cabinNo, emp);
if(emp.getDeparment() != null){
emp.getDeparment().removeEmpFromDept(emp);
}
emp.setDeparment(this);
}
public void removeEmpFromDept(Employee emp) {
Iterator<Map.Entry<String, Employee>> entrySet = getEmpCabin().entrySet().iterator();
if(entrySet.hasNext()){
Employee current = ((Map.Entry<String, Employee>) entrySet.next()).getValue();
if(current.getId() == emp.getId()){
System.out.println("dept remove");
entrySet.remove();
current.setDeparment(null);
}
}
}
my service class function
public class EmployeeService {
private EntityManager em;
.............
public void setEmpCabin(Employee emp , Department dept , String cab_id) {
dept.assignEmpToCabin(emp, cab_id);
}
My main code :
tr.begin();
Department dept = deps.createDepartment("Sci");
Employee emp = es.createEmployee("Harmeet Singh", "500" , "5684", dept);
es.setEmpCabin(emp, dept, "10");
tr.commit();
when i try to insert the data using setEmpCabin(...) function , it wil generate the exception
Caused by: org.hibernate.exception.GenericJDBCException: Field 'cabin_id' doesn't have a default value

Related

MongoDB repository save() do the insert instead of update

I am writing a PUT request API with spring and mongodb. But the save() inserts a new object instead of update the current one.
#Document("Test")
public class Expense {
#Field(name = "name")
private String expenseName;
#Field(name = "category")
private ExpenseCategory expenseCategory;
#Field(name = "amount")
private BigDecimal expenseAmount;
public Expense( String expenseName, ExpenseCategory expenseCategory, BigDecimal expenseAmount) {
this.expenseName = expenseName;
this.expenseCategory = expenseCategory;
this.expenseAmount = expenseAmount;
}
public String getExpenseName() {
return expenseName;
}
public void setExpenseName(String expenseName) {
this.expenseName = expenseName;
}
public ExpenseCategory getExpenseCategory() {
return expenseCategory;
}
public void setExpenseCategory(ExpenseCategory expenseCategory) {
this.expenseCategory = expenseCategory;
}
public BigDecimal getExpenseAmount() {
return expenseAmount;
}
public void setExpenseAmount(BigDecimal expenseAmount) {
this.expenseAmount = expenseAmount;
}
}
This is my reporsitory class
public interface ExpenseRepository extends MongoRepository<Expense, String> {
}
This is my Service class which shows how to update the class.
#Service
public class ExpenseService {
private final ExpenseRepository expenseRepository;
public ExpenseService(ExpenseRepository expenseRepository) {
this.expenseRepository = expenseRepository;
}
public void updateExpense(String id, Expense expense){
Expense savedExpense = expenseRepository.findById(id)
.orElseThrow(() -> new RuntimeException(
String.format("Cannot Find Expense by ID %s", id)));
savedExpense.setExpenseName(expense.getExpenseName());
savedExpense.setExpenseAmount(expense.getExpenseAmount());
savedExpense.setExpenseCategory(expense.getExpenseCategory());
expenseRepository.save(savedExpense);
}
}
This is my controller
#RestController
#RequestMapping("/api/expense")
public class ExpenseController {
private final ExpenseService expenseService;
public ExpenseController(ExpenseService expenseService) {
this.expenseService = expenseService;
}
#PutMapping("/{id}")
public ResponseEntity<Object> updateExpense(#PathVariable String id, #RequestBody Expense expense){
expenseService.updateExpense(id, expense);
return ResponseEntity.ok().build();
}
}
As shown in mongodb compass, mongodb auto generates an _id field for every object. So I do not define a id field or use #id annotation to define a primary for the collection. However, in the service class, expenseRepository.findById(id) retrieves the desired object and update it. Why does save() do the insert instead of update? Many thanks.
JPA Can't find the existing entry as no id field id set. You need to add an id field and set generation type to auto.
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;

#Transactional does not work as expected, because the "save" method is needed to save to the database

#Transactionalshould itself reflect the changes made to the entity in the database.
I'm creating an application where the client can create a Car entity that looks like this (the update method is later used by PUT, do not pay attention to the brand property):
#Entity
#Table(name = "cars")
public class Car {
#Id
#GeneratedValue(generator = "inc")
#GenericGenerator(name = "inc", strategy = "increment")
private int id;
#NotBlank(message = "car name`s must be not empty")
private String name;
private LocalDateTime productionYear;
private boolean tested;
public Car() {
}
public Car(#NotBlank(message = "car name`s must be not empty") String name, LocalDateTime productionYear) {
this.name = name;
this.productionYear = productionYear;
}
#ManyToOne
#JoinColumn(name = "brand_id")
private Brand brand;
public int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getProductionYear() {
return productionYear;
}
public void setProductionYear(LocalDateTime productionYear) {
this.productionYear = productionYear;
}
public boolean isTested() {
return tested;
}
public void setTested(boolean tested) {
this.tested = tested;
}
public Brand getBrand() {
return brand;
}
void setBrand(Brand brand) {
this.brand = brand;
}
public Car update(final Car source) {
this.productionYear = source.productionYear;
this.brand = source.brand;
this.tested = source.tested;
this.name = source.name;
return this;
}
}
In my application, the client can create a new Car or update an existing one with the PUT method.
My controller:
#RestController
public class CarController {
private Logger logger = LoggerFactory.getLogger(CarController.class);
private CarRepository repository;
public CarController(CarRepository repository) {
this.repository = repository;
}
//The client can create a new resource or update an existing one via PUT
#Transactional
#PutMapping("/cars/{id}")
ResponseEntity<?> updateCar(#PathVariable int id, #Valid #RequestBody Car source) {
//update
if(repository.existsById(id)) {
repository.findById(id).ifPresent(car -> {
car.update(source); //it doesn`t work
//Snippet below works
//var updated = car.update(source);
//repository.save(updated);
});
return ResponseEntity.noContent().build();
}
//create
else {
var result = repository.save(source);
return ResponseEntity.created(URI.create("/" + id)).body(result);
}
}
}
When I create a new Car, it works. However as described in the code, when there is no save method the entity is not changed although I get the status 204 (no content). When there is a save method, it works fine.
Do you know why this is so?
One of the users asked me for a Brand entity. I haven't created any Brand object so far but essentially Car can belong to a specific Brand in my app. So far, no Car belongs to any Brand. Here is this entity:
#Entity
#Table(name = "brands")
public class Brand {
#Id
#GeneratedValue(generator = "i")
#GenericGenerator(name = "i", strategy = "increment")
private int id;
#NotBlank(message = "brand name`s must be not empty")
private String name;
private LocalDateTime productionBrandYear;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "brand")
private Set<Car> cars;
#ManyToOne
#JoinColumn(name = "factory_id")
private Factory factory;
public Brand() {
}
public int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getProductionBrandYear() {
return productionBrandYear;
}
public void setProductionBrandYear(LocalDateTime productionBrandYear) {
this.productionBrandYear = productionBrandYear;
}
public Set<Car> getCars() {
return cars;
}
public void setCars(Set<Car> cars) {
this.cars = cars;
}
public Factory getFactory() {
return factory;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
}
I tried your entities with same use case locally and found out everything is working fine, I am writing here my findings and configurations so that you can verify what's going on wrong for you.
So, when I issue a PUT call providing id but Car entity doesn't exist into table, it gets created and I receive 201 response (I guess you are getting the same)
you can see that row with value got inserted into table as well
and these are the query logs printed
- [nio-8080-exec-8] org.hibernate.SQL: select count(*) as col_0_0_ from car car0_ where car0_.id=?
[nio-8080-exec-8] org.hibernate.SQL: select car0_.id as id1_1_0_, car0_.brand_id as brand_id5_1_0_, car0_.name as name2_1_0_, car0_.production_year as producti3_1_0_, car0_.tested as tested4_1_0_ from car car0_ where car0_.id=?
[nio-8080-exec-8] org.hibernate.SQL: insert into car (brand_id, name, production_year, tested) values (?, ?, ?, ?)
Now, let's come to updating the same entity, when issued PUT request for same id with changed values notice that values changes in table and update queries in log
You can see that got same 204 response with empty body, let's look the table entry
So changes got reflected in DB, let's look at the SQL logs for this operation
select count(*) as col_0_0_ from car car0_ where car0_.id=?
[nio-8080-exec-1] org.hibernate.SQL: select car0_.id as id1_1_0_, car0_.brand_id as brand_id5_1_0_, car0_.name as name2_1_0_, car0_.production_year as producti3_1_0_, car0_.tested as tested4_1_0_, brand1_.id as id1_0_1_, brand1_.name as name2_0_1_, brand1_.production_year as producti3_0_1_ from car car0_ left outer join brand brand1_ on car0_.brand_id=brand1_.id where car0_.id=?
[nio-8080-exec-1] org.hibernate.SQL: update car set brand_id=?, name=?, production_year=?, tested=? where id=?
So, I am not sure, how you verified and what you verified but your entities must work, I have used same controller function as yours
#RestController
class CarController {
private final CarRepository repository;
public CarController(CarRepository repository) {
this.repository = repository;
}
#PutMapping("/car/{id}")
#Transactional
public ResponseEntity<?> updateCar(#PathVariable Integer id, #RequestBody Car source) {
if(repository.existsById(id)) {
repository.findById(id).ifPresent(car -> car.update(source));
return ResponseEntity.noContent().build();
}else {
Car created = repository.save(source);
return ResponseEntity.created(URI.create("/" + created.getId())).body(created);
}
}
}
Possible differences from your source code could be as follow:
I used IDENTITY generator to generate the PRIMARY KEY, instead of the one you have on your entity as it was easy for me to test.
I provided ObjectMapper bean to serialize/deserialize the request body to Car object to support Java 8 LocalDateTime conversion, you may have your way to send datetime values, so that it converts to Car Object.
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
// And Object mapper bean
#Bean
public static ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper;
}
However, these differences should not matter.
application.properties
To print query logs to verify if queries are fired or not
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.jpa.show-sql=true
spring.jpa.open-in-view=false
logging.level.org.hibernate.SQL=DEBUG
The fact that you are updating the car object doesn't mean it updates the value in the DB. You always need to call repository.save() method to persist your changes in the DB.

How to send only the ID the of main nested objects in the body request in spring boot

I'm creating eCommerce for merchants using spring boot with JPA.
I have an issue while creating the order service.
I want to only pass the ID of the nested objects in the request body instead of sending the full nest objects because the size will be extremely big.
Here is my code.
Merchant can do many orders
Order
#Entity
#Table(name = "Orders")
#XmlRootElement
#JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Order extends BasicModelWithIDInt {
#Basic(optional = false)
#Column(name = "Quantity")
private Integer quantity;
#Basic(optional = false)
#Size(min = 1, max = 150)
#Column(name = "Notes")
private String notes;
#JoinColumn(name = "ProductID", referencedColumnName = "ID")
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#JsonIgnoreProperties
private Product productID;
#JoinColumn(name = "MerchantID", referencedColumnName = "ID")
#ManyToOne(optional = false, fetch = FetchType.LAZY)
private Merchent merchent;
#JoinColumn(name = "OrderSatusID", referencedColumnName = "ID")
#ManyToOne(optional = false, fetch = FetchType.EAGER)
private OrderStatus orderStatus;
// Getters and Setters
}
Order Holder
public class OrderHolder {
#NotNull
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderRepo
public interface OrderRepo extends JpaRepository<Order, Integer> {
}
Order Controller
#RestController
#RequestMapping(value = "order", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderRestController extends BasicController<OrderHolder>{
#Autowired
private OrderRepo orderRepo;
#PostMapping("create")
public ResponseEntity<?> create(#RequestBody #Valid OrderHolder orderHolder, Principal principal) throws GeneralException {
log.debug( "create order {} requested", orderHolder.toString());
Order order = new Order();
order = orderHolder.getOrder();
System.out.println("###############"+order);
try {
order = orderRepo.save(order);
log.info( "Order {} has been created", order );
} catch (Exception e) {
log.error( "Error creating Order: ", e );
e.printStackTrace();
throw new GeneralException( Errors.ORDER_CREATION_FAILURE, e.toString() );
}
return ResponseEntity.ok( order );
}
}
I need request body to look like the below instead of including the full Merchant and Product objects inside the request.
You can make use of JsonView to return only id of product and merchant
public class OrderView {}
...
public class Product{
#Id
#JsonView(OrderView.class)
private Integer id
private String otherFieldWithoutJsonView
...
}
and then in your controller
#PostMapping("create")
#JsonView(OrderView.class) // this will return the product object with one field (id)
public ResponseEntity<?> create(#RequestBody #Valid OrderHolder orderHolder, Principal principal) throws GeneralException {
...
}
hope this can help you
Just have a separate contract class.
public class OrderContract {
private int merchantID;
private String notes;
....
//getter, setters
}
public class OrderHolder {
#NotNull
private OrderContract orderContract;
public OrderContract getOrderContract() {
return orderContract;
}
public void setOrder(OrderContract orderContract) {
this.orderContract = orderContract;
}
}
And before making a call to the Repository , translate from OrderContract to Order.
I would like to share something regarding this.
I have searched a lot on internet and tried lot of things, but the solution given here suited well for this scenario.
https://www.baeldung.com/jackson-deserialization
You need to create a Custom-deserializer for your model by extending StdDeserializer from com.fasterxml.jackson.databind.deser.std.StdDeserializer, where you just want to pass id's and not the whole object in the request.
I have given below example for User Model with Address object.
User(long userId, String name, Address addressId)
Address(long addressId, String wholeAddress)
Writing Deserializer for User class
public class UserDeserializer extends StdDeserializer<User> {
public User() {
this(null);
}
public User Deserializer(Class<?> vc) {
super(vc);
}
#Override
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode node = p.getCodec().readTree(p);
long id = 0;
long addressId = (Long) ((IntNode) node.get("addressId")).numberValue().longValue();
return new User(id, name, new Address(addressId, null)
}
Now you have to use
#JsonDeserialize(using = UserDeserializer.class)
public Class User {
...
}
POST request
Before custom deserialization
{
"name" : "Ravi",
"addressId" : { "id" : 1}
}
After custom Deserialization
{
"name" : "Ravi",
"addressId" : 1
}
Also while GET /user/:id call you will get the whole obj like
{
"name" : "Ravi",
"addressId" : { "id" : 1, "wholeAddress" : "Some address"}
}

How to returnDTO instead of entity in Spring boot pagination?

My DTO is different from entity. How can I return a DTO instead of entity with pagination while still showing information of all pages?
Controller:
#GetMapping("/{name}")
public Page<Student> getStudent(#PathVariable(value = "name") String name, Pageable pageable){
Page <Student> page = studentService.getStudent(name, pageable);
return page;
}
Service:
public Page<Student> getStudent(String name, Pageable pageable){
Page<Student> students = studentRepository.findAllByName(name, pageable);
return students;
}
Repository:
#Repository
public interface StudentRepository extends
PagingAndSortingRepository<Student, Long> {
Page<Student> findAllByName(String name, Pageable pageable);
}
DTO:
#Data
public class StudentDTO extends ResourceSupport {
Long _id;
String name;
}
Entity:
#Entity
#Data
#NoArgsConstructor(force = true, access = AccessLevel.PUBLIC)
public class Student {
#Id
#GeneratedValue
private Long id;
private String name;
private Long grade;
}
The StudentDTO class can have a constructor with a Student parameter.
public StudentDTO(Student student) {
this._id = student.getId();
this.name = student.getName();
}
Then you can call map on the Page object.
Page<StudentDTO> dtoPage = page.map(student -> new StudentDTO(student));
Please look here - this is an approach of how to work with DTO in Spring Data REST projects
You can do as follows:
#Query("SELECT new StudentDTO(student.id, student.name) FROM Student student "
+ "WHERE student.name like :name ")
List<StudentDTO> findAllCustomBy(#Param("name") String name)
and then you create a Constructor inside StudentDto
public class StudentDto {
public StudentDto(Long id, String name){
this.id = id;
this.name = name;
}
Pageable pageable = PageRequest.of(pageNo,PAGE_SIZE_12);
Page<StudentDTO> studentDTO = new PageImpl<>(studentDTO ,pageable,studentDTO.size());

Using #IdClass for storing entity with composite primary key, but fails to persist

my id class as follows,
public class EmployeeId implements Serializable{
public EmployeeId(){}
public EmployeeId(Integer id, String country){
this.id = id;
this.country = country;
}
private Integer id;
private String country;
#Override
public int hashCode(){
return this.getCountry().hashCode() + getId();
}
#Override
public boolean equals(Object o){
boolean flag = false;
EmployeeId myId = (EmployeeId) o;
if((o instanceof EmployeeId)
&& (this.getCountry().equals(myId.getCountry()))
&& (this.id == myId.getId())){
flag = true;
}
return flag;
}
// rest of the code with getters only
}
Following is my entity using
#Entity
#IdClass(EmployeeId.class)
#Table(name="TBL_EMPLOYEE_FOUR")
public class EmployeeEntityTwo {
public EmployeeEntityTwo(){}
public EmployeeEntityTwo(Integer id,String country, String empName){
this.country = country;
this.employeeId = id;
this.empName = empName;
}
#Id
#Column(name="ID")
private Integer employeeId;
#Id
#Column(name="COUNTRY",length=50)
private String country;
#Column(name="NAME",length=50)
private String empName;
// getters and setters
}
This is my table
create table TBL_EMPLOYEE_FOUR(
ID integer,
COUNTRY varchar(50),
NAME varchar(50),
constraint PK_EMP_00239 primary key(ID,COUNTRY)
)
This is what i am trying to run
private static void idClassStore(EntityManager em) throws Exception{
List<EmployeeEntityTwo> employees = Arrays.asList(new EmployeeEntityTwo(12, "KENYA", "Ridushi Ogambe"),
new EmployeeEntityTwo(13, "GHANA", "Mikila Hanza"),
new EmployeeEntityTwo(14, "EGYPT", "Abdul Hameed Fagdaul"),
new EmployeeEntityTwo(15, "MOROCCO", "Jamil Mahmoud"),
new EmployeeEntityTwo(16, "LIBERIA", "Robert Damus"));
for(EmployeeEntityTwo employee : employees){
em.persist(employee);
}
}
But i get an exception as
Caused by: org.hibernate.AnnotationException: Property of #IdClass not found in entity com.entities.EmployeeEntityTwo: id
I am using JPA with Hibernate as persistence provider,
But #IdClass which i have used is of import javax.persistence.IdClass;
so where is am going wrong,
I have discovered the solution:
The 'id' field in the classes EmployeeEntityTwo and EmployeeId should be same.
// EmployeeId.java;
private Integer id;
should be
// EmployeeId.java;
private Integer employeeId;
I adjusted the getter respectively, and it worked.
From javax.persistence.IdClass JavaDocs:
The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.

Categories