I want set two different tables.i have try this first model with corresponding service,controller and respository.Everythings is fine but when i use the same code with only changing the model,and develop a set of service,controller and respository.It cannot show what i expected.
my model:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
@Data
@AllArgsConstructor
@NoArgsConstructor
@javax.persistence.Entity
@Table(schema = "order")
public class Order {
@Id
@GeneratedValue
private int id;
private String product_name;
private int quantity;}
mycontroller:
package com.javatechie.trymysql.contoller;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class OrderController {
@Autowired
private OrderService service;
@PostMapping("/add/orders")
public List<Order> addOrders(@RequestBody List<Order> orders){
return service.saveOrders(orders);
}
}
service:
package com.javatechie.trymysql.service;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.repository.OrderRepository;
import com.javatechie.trymysql.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService {
@Autowired
private OrderRepository Repository;
public List<Order> saveOrders(List<Order> order) {
return Repository.saveAll(order);
}
}
repository:
package com.javatechie.trymysql.repository;
import com.javatechie.trymysql.Entity.Order;
import com.javatechie.trymysql.Entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface OrderRepository extends JpaRepository<Order,Integer> {
}
my ide pattern is like:
com.javatechie.trymysql
-entity
--Product
--Order
-controller
--productcontroller
--ordercontroller
-service
--productservice
--orderservice
-repository
--productrespository
--orderreposiory
-productconfig
-Trymysqlapplication
Postman request:
[{
"proudct_name":"sand",
"quantity":5
},{
"product_name":"fruit",
"quantity":2
}]
error:
{
"timestamp": "2022-10-17T13:14:03.284+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/add/orders"
}
Everytime i run the server it display the following log:
Hibernate: select product0_.id as id1_1_0_, product0_.name as name2_1_0_, product0_.price as price3_1_0_, product0_.quantity as quantity4_1_0_ from product_tbl product0_ where product0_.id=?
Hibernate: select product0_.id as id1_1_0_, product0_.name as name2_1_0_, product0_.price as price3_1_0_, product0_.quantity as quantity4_1_0_ from product_tbl product0_ where product0_.id=?
But it still works fine
later i add the second table it just show me: Hibernate: select order0_.id as id1_0_0_, order0_.product_name as product_2_0_0_, order0_.quantity as quantity3_0_0_ from order order0_ where order0_.id=? 2022-10-17 21:21:11.916 WARN 3828 --- [nio-9191-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
i dont why my log file automatically generate this sql and got the error when it is not the model "product"? I dont know which step i have done wrong when trying using the second model(order)
Extracted details from comments.
As you have table name
orderwhich is a reserved keyword in MySQL. So you have to change the table name to another name that is not a reserved keyword. Change toordersresolves the issue.