Here is my entity class
#Entity
#Table( name = "NEO_TEAM", schema = "METRICS" )
public class ETeam implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="ID")
private int id;
#Column(name="NAME")
private String name;
#Column(name="DESCRIPTION")
private String description;
//bi-directional many-to-one association to ETeamQueue
#OneToMany(mappedBy="eteam" , fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<ETeamQueue> teamQueue;
public ETeam(int id,String name,String description){
this.id = id;
this.name = name;
this.description = description;
}
public ETeam(String name,String description){
this.name = name;
this.description = description;
}
public ETeam() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public List<ETeamQueue> getTeamQueue() {
return this.teamQueue;
}
public void setTeamQueue(List<ETeamQueue> teamQueue) {
this.teamQueue = teamQueue;
}
public ETeamQueue addTeamQueue(ETeamQueue teamQueue) {
getTeamQueue().add(teamQueue);
teamQueue.setEteam(this);
return teamQueue;
}
public ETeamQueue removeTeamQueue(ETeamQueue teamQueue) {
getTeamQueue().remove(teamQueue);
teamQueue.setEteam(null);
return teamQueue;
}
}
And My REST call and JPQL query is
#Path("team")
#Produces(MediaType.APPLICATION_JSON)
public class TeamResource {
TeamService ts = new TeamService();
#GET
#Path("/{id}")
#Produces(MediaType.APPLICATION_JSON)
public List<ETeam> listTeam(#PathParam("id") int id){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("NeoMetrics");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("select e from ETeam e where e.id = :id ",ETeam.class);
query.setParameter("id", id);
List<ETeam> lis = (List<ETeam>) query.getResultList();
//List<ETeam> lis = ts.getTeam(id);
return lis;
}
i want to fetch only one record from the team table but it give me result like shown in picture , which show the result is looped thousand times , its very long output result i only put part of it , any help will be really appreciated
Related
I just get started with JPA. I have an entity CustomerOrderEntity which has #OneToMany relation with another entity BeverageEntity. When I add the BeverageEntity to the CustomerOrderEntity a new entity is created of the type BeverageEntity and is persisted in the database as a new record. I do not want something like that I only need the CustomerOrderEntity to have the record of BeverageEntity and a new entity is not created for that inside the database.
These two entities are invoked from this class SalesManagement:
#Stateless
#Remote(SalesManagement.class)
public class SalesManagementBean implements SalesManagement {
#PersistenceContext(type = PersistenceContextType.TRANSACTION)
EntityManager em;
#Override
public void createOrder(CustomerOrder order) {
order.getOrderItems().forEach(item -> {
updateBeverageQuantity(item.getId(), item.getQuantity());
});
CustomerOrderEntity customerOrderEntity = new CustomerOrderEntity();
customerOrderEntity.setIssueDate(order.getIssueDate());
order.getOrderItems().forEach(item ->{
customerOrderEntity.addOrder(toEntity(item));
});
em.persist(customerOrderEntity);
}
public void updateBeverageQuantity(int b_id, int quantity) {
BeverageEntity beverageEntity = em.find(BeverageEntity.class, b_id);
if(beverageEntity != null) {
beverageEntity.setQuantity(beverageEntity.getQuantity() - quantity);
em.persist(beverageEntity);
}
}
public List<BeverageEntity> toBeverageEntity(List<Beverage> beverages) {
List<BeverageEntity> entities = new ArrayList<BeverageEntity>();
if(beverages != null && beverages.size() > 0) {
beverages.forEach(beverage -> {
BeverageEntity entity = new BeverageEntity();
entity.setName(beverage.getName());
entity.setManufacturer(beverage.getManufacturer());
entity.setQuantity(beverage.getQuantity());
entity.setPrice(beverage.getPrice());
entity.setIncentiveEntity(toIncentiveEntity(beverage.getIncentiveDTO()));
entities.add(entity);
});
return entities;
}
return null;
}
public IncentiveEntity toIncentiveEntity(IncentiveDTO incentiveDTO) {
if(incentiveDTO != null) {
IncentiveEntity entity = null;
return entity;
}
return null;
}
public BeverageEntity toEntity(Beverage b) {
BeverageEntity entity = new BeverageEntity();
entity.setName(b.getName());
entity.setManufacturer(b.getManufacturer());
entity.setQuantity(b.getQuantity());
entity.setPrice(b.getPrice());
entity.setIncentiveEntity(toIncentiveEntity(b.getIncentiveDTO()));
return entity;
}
}
I think the problem is in the method toBeverageEntity. But I am not sure about it.
The CustomerOrderEntity:
#Entity
public class CustomerOrderEntity {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
#Version
private int version;
private Date issueDate;
#OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private List<BeverageEntity> beverageEntities;
public void addOrder(BeverageEntity beverageEntity) {
this.beverageEntities.add(beverageEntity);
}
public List<BeverageEntity> getBeverageEntities() {
return beverageEntities;
}
public void setBeverageEntities(List<BeverageEntity> beverageEntities) {
this.beverageEntities = beverageEntities;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public Date getIssueDate() {
return issueDate;
}
public void setIssueDate(Date issueDate) {
this.issueDate = issueDate;
}
}
The BeverageEntity:
#Entity
public class BeverageEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String name;
private String manufacturer;
private int quantity;
private double price;
#Version
private int version;
#ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private IncentiveEntity incentiveEntity;
public BeverageEntity() {
}
public BeverageEntity(String name, String manufacturer, int quantity, Double price, IncentiveEntity entity) {
this.name = name;
this.manufacturer = manufacturer;
this.quantity = quantity;
this.price = price;
this.incentiveEntity = entity;
}
public BeverageEntity(String name, String manufacturer, int quantity, Double price) {
this.name = name;
this.manufacturer = manufacturer;
this.quantity = quantity;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public IncentiveEntity getIncentiveEntity() {
return incentiveEntity;
}
public void setIncentiveEntity(IncentiveEntity incentiveEntity) {
this.incentiveEntity = incentiveEntity;
}
}
Found the problem it was in the method toBeverageEntity as pointed out in the comments I was creating a new entity that was wrong. I needed to use find because the entity is already in the database.
BeverageEntity entity = em.find(BeverageEntity.class, beverage.getId());
entities.add(entity);
I have a User Entity and an Order Entity.
One of the field in order entity is date.
Till now i have the user enter the date.
Now i want that at the time post request is made the date is automatically set to the current date and stored in the database.
Tried using #Prepersist annotation But since this is my first API that i am developing using springBoot , I don't really know how to use it.
User Entity
#Entity
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;
#NotEmpty(message = "Name is compulsory")
#Pattern(regexp="^[A-Za-z]*[A-Za-z-'. ]*[A-Za-z]*$",message = "Name has invalid characters")
private String username;
//#NotEmpty(message = "Phone Number is compulsary")
#Range(min = 6400000000L ,max=9999999999L)
private Long phoneNumber;
#NotEmpty(message = "Address is compulsary")
private String address;
public User(){}
public User(BigInteger id, String username, Long phoneNumber, String address) {
super();
this.id = id;
this.username = username;
this.phoneNumber = phoneNumber;
this.address = address;
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(Long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Order Entity
#Entity
public class Orders {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;
#CreationTimestamp
#Temporal(TemporalType.TIMESTAMP)
private Date date;
#ManyToOne
private User user;
public Orders(){}
public Orders(BigInteger id, Date date,BigInteger userId) {
super();
this.id = id;
this.date = date;
this.user=new User(userId," ",0000000000L," ");
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Order Controller
#RestController
public class OrdersController {
#Autowired
private OrdersService ordersService;
#ApiOperation(value="Show all orders")
#RequestMapping("/orders")
public Iterable<Orders> getAllOrders()
{
return ordersService.getAllOrders();
}
#ApiOperation(value="Show a particular Order")
#RequestMapping("/orders/{orderId}")
public Orders getOrderById(#PathVariable BigInteger orderId)
{
return ordersService.getOrderById(orderId);
}
#ApiOperation(value="Show all orders of a particular User")
#RequestMapping("/users/{id}/orders")
public List<Orders> getOrders(#PathVariable BigInteger id) {
return ordersService.getOrders(id);
}
#ApiOperation(value="Show an order for a User")
#RequestMapping("/users/{userId}/orders/{id}")
public Orders getOrder(#PathVariable BigInteger id){
return ordersService.getOrder(id);
}
#ApiOperation(value="Adds a new Order")
#RequestMapping(method = RequestMethod.POST,value = "/users/{userId}/orders")
public Orders addOrder(#PathVariable BigInteger userId,#RequestBody Orders orders) {
orders.setUser(new User(userId," ",0000000000L," "));
return ordersService.addOrder(orders);
}
#ApiOperation(value="Alter an Order")
#RequestMapping(method = RequestMethod.PUT, value="/users/{userId}/orders/{id}")
public Orders updateOrder(#RequestBody Orders order,#PathVariable BigInteger id,#PathVariable BigInteger userId)throws Exception {
order.setUser(new User(userId," ",0000000000L," "));
return ordersService.updateOrder(order, id);
}
#ApiOperation(value="Delete an Order")
#RequestMapping(method = RequestMethod.DELETE, value="/orders/{id}")
public void deleteOrder(#PathVariable BigInteger id){
ordersService.deleteOrder(id);
}
}
#PrePersist is a JPA annotation and therefore should work in all compatible persistence frameworks. It indicates a method that should be invoked on particular entity lifecycle event. (Other events are well documented in the Hibernate user guide here).
Add this to your entity:
#Temporal(TemporalType.TIMESTAMP)
#Column(nullable = false)
private Date timestamp;
#PrePersist
private void onCreate() {
timestamp = new Date();
}
As for assigning/creating the entity in the controller, it is a good practice to use DTO (data transfer objects) in your controller (#RequestBody OrderDto orderDto) and then use some method to populate a new entity instance with those values. Most common options are
modelmapper
manually
...
Order o = new Order();
o.user = userDao.findById(orderDto.getUserId());
...
// persist o
Not sure why I have an issue here, but when I save with a CrudRepository with these objects, I get the SerializationException (with no further information). Can someone take a look at my objects and offer me some insight into why they can't serialize? My pom.xml is attached last as well in case that helps somehow. I'm using a Postgres database.
EDIT: The database and now - tables are created, but objects are not creating rows.
The actual CrudRepository interface:
public interface AccountRepository extends CrudRepository<ZanyDishAccount, String> {}
ZanyDishAccount entity:
#Entity
public class ZanyDishAccount {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id; // internal id of the customer account for a Zany Dish subscription
private String status;
#OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "company_id")
private Company company;
#OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "order_id")
private Order order;
public ZanyDishAccount() {}
public ZanyDishAccount(Company company, Order order) {
this.company = company;
this.order = order;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+ ", company = " + company + ", status = " + status + "]";
}
}
Company entity:
#Entity
public class Company {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
Long id;
private String phoneNumber;
private String website;
private String name;
private String uuid;
private String country;
public Company() {}
public Company(String phoneNumber, String website, String name, String uuid, String country) {
this.phoneNumber = phoneNumber;
this.website = website;
this.uuid = uuid;
this.country = country;
}
public String getPhoneNumber ()
{
return phoneNumber;
}
public void setPhoneNumber (String phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public String getWebsite ()
{
return website;
}
public void setWebsite (String website)
{
this.website = website;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public String getUuid ()
{
return uuid;
}
public void setUuid (String uuid)
{
this.uuid = uuid;
}
public String getCountry ()
{
return country;
}
public void setCountry (String country)
{
this.country = country;
}
#Override
public String toString()
{
return "ClassPojo [phoneNumber = "+phoneNumber+", website = "+website+", name = "+name+", uuid = "+uuid+", country = "+country+"]";
}
}
Order entity:
#Entity
#Table(name = "_order")
public class Order {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
Long id;
private String pricingDuration;
private Items[] items;
private String editionCode;
public Order() {}
public Order(String pricingDuration, Items[] items, String editionCode) {
this.pricingDuration = pricingDuration;
this.items = items;
this.editionCode = editionCode;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPricingDuration ()
{
return pricingDuration;
}
public void setPricingDuration (String pricingDuration)
{
this.pricingDuration = pricingDuration;
}
public Items[] getItems ()
{
return items;
}
public void setItems (Items[] items)
{
this.items = items;
}
public String getEditionCode ()
{
return editionCode;
}
public void setEditionCode (String editionCode)
{
this.editionCode = editionCode;
}
#Override
public String toString()
{
return "ClassPojo [pricingDuration = "+pricingDuration+", items = "+items+", editionCode = "+editionCode+"]";
}
}
Thanks for your help!
Mike
Hm, this seems multi-faceted. Let's see if I can help at all. Last thing first...
No tables being created automatically.
I would take a look at this section in Spring's docs for the most basic approach: Initialize a database using Hibernate. For example, spring.jpa.hibernate.ddl-auto: create-drop will drop and re-create tables each time the application runs. Simple and easy for initial dev work. More robust would be leveraging something like Flyway or Liquibase.
Serialization issue
So without logs, and the fact that you have no tables created, the lack of a persistence layer would be the assumed culprit. That said, when you have tables and data, if you do not have a repository for all of the related tables, you'll end up with a StackOverflow error (the serialization becomes circular). For that, you can use #JsonBackReference (child) and #JsonManagedReference (parent). I have been successful using only #JsonBackReference for the child.
Items[]
I'm not sure what Item.class looks like, but that looks like an offensive configuration that I missed the first round.
Change private Items[] items; to private List<Item> items = new ArrayList<Item>();. Annotate with #ElementCollection.
Annotate Item.class with #Embeddable.
I'm using Spring data jpa and i am trying to do this :
#RequestMapping(value = "/setview/{id}", method = RequestMethod.GET)
public Iterable<Task> setView(#PathVariable Integer id) {
System.out.println("setViewTrue -------------------");
Iterable<Task> tasks = taskRepository.findByUserId(id);
for (Task t : tasks) {
t.setView(true);
taskRepository.save(t);
System.out.println("task****: "+ t.isView());
}
return tasks;
}
but i got this error:
[Ljava.lang.Object; cannot be cast to com.yess.erp.crm.domain.Task
i just want to loop an iterbale of tasks and change the value of a boolean(false) to true.
this is my Task.java:
#Entity
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "pk_sequence", sequenceName = "task_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "pk_sequence")
private Integer id;
#NotEmpty
private String title;
#Lob
private byte[] image;
private Date created_at;
private Date start_date;
private Date end_date;
private String description;
private boolean view;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id", nullable = false)
private User user;
public Task() {
}
public Task(String title, User user) {
super();
this.title = title;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public Date getStart_date() {
return start_date;
}
public void setStart_date(Date start_date) {
this.start_date = start_date;
}
public Date getEnd_date() {
return end_date;
}
public void setEnd_date(Date end_date) {
this.end_date = end_date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isView() {
return view;
}
public void setView(boolean view) {
this.view = view;
}
}
this is my TaskRepository.java:
public interface TaskRepository extends CrudRepository<Task, Integer> {
#Query("from Task as t inner join t.user as u where u.id = :id AND t.view = false")
Iterable<Task> findByUserId(#Param("id") Integer id);
}
this is my TaskController.java:
#RestController
#RequestMapping("/tasks")
public class TaskController {
#Autowired
private TaskRepository taskRepository;
.
.
.
#RequestMapping(value = "/setview/{id}", method = RequestMethod.GET)
public Iterable<Task> setView(#PathVariable Integer id) {
System.out.println("setViewTrue -------------------");
Iterable<Task> tasks = taskRepository.findByUserId(id);
for (Task t : tasks) {
t.setView(true);
taskRepository.save(t);
System.out.println("task****: "+ t.isView());
}
return tasks;
}
}
Your query isn't returning just a task, it is likely returning a task and user, in an Object[] array.
You might be able to alter your query to get a Task back. I'm thinking SELECT t FROM Task t...
I have a One-to-Many relationship: A ProductCategory can contains many Product. This is the code:
#Entity
public class Product implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private String id;
#Column(name="ProductName")
private String name;
private BigDecimal price;
private String description;
#ManyToOne
#JoinColumn(name="UserId")
private User user;
#ManyToOne
#JoinColumn(name="Category")
private ProductCategory category;
private static final long serialVersionUID = 1L;
public Product() {
super();
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public ProductCategory getCategory() {
return this.category;
}
public void setCategory(ProductCategory category) {
this.category = category;
}
}
#Entity
public class ProductCategory {
#Id
private String categoryName;
#OneToMany(cascade= CascadeType.ALL,mappedBy="category")
private List<Product> products;
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String productName) {
this.categoryName = productName;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
This is Servlet code which use the 2 entities:
String name = request.getParameter("name");
BigDecimal price = new BigDecimal(request.getParameter("price"));
String description = request.getParameter("description");
ProductCategory category = new ProductCategory();
category.setCategoryName(request.getParameter("category"));
Product product = new Product();
product.setName(name);
product.setPrice(price);
product.setDescription(description);
product.setCategory(category);
User user = userManager.findUser("Meow");
product.setUser(user);
productManager.createProduct(product); // productManager is an EJB injected by container
And this is the error:
java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST
Why does this error happen? I marked the field as "cascade = CascadeType.All"!
You're trying to save a product. And this product is linked to a category. So when JPA saves the product, its category must already exist, or there must be a cascade configured so that persisting the product cascades to persisting its category.
But you don't have such a cascade. What you have is a cascade saying that any operation done on a category cascades to its list of products.