Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to Spring Boot and still learning concepts such as JPA and Hibernate. I was practicing CRUD operations and got an error that totally confused me. I have a project with 4 entities, Admin, Customer, Product, and Saleorder. There is an one more entity, named "Order" which was the original order entity but because it was not working so I had to create "Saleorder".

Now the problem is that both the classes Saleorder and Order contains exactly the same code, the only difference is in the name. But while Saleorder works the Order class doesn't work as expected. When I run my springboot application with Order class (Saleorder commented) only Admin, Customer, Product and Hibernate Sequence tables are created but not the Order table. And when I copy the same code to Saleorder class and commented Order class then JPA Hibernate creates all the required tables viz. Admin, Product, Saleorder, Customer and Product_Saleorder.

Following is the code contained in my classes

Java
package com.sportyshoes.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Admin {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	private String name;
	
	@Column(unique = true)
	private String email;
	
	private String password;
}


Java
package com.sportyshoes.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Customer {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	private String name;
	
	@Column(unique = true)
	private String email;
	
	private String password;
	
	private String address;
	
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
	private List<Saleorder> saleorders;
	
}



package com.sportyshoes.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Saleorder {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	@ManyToOne(cascade = CascadeType.ALL)
	private Customer customer;
	
	@ManyToMany(cascade = CascadeType.ALL, mappedBy = "saleorders")
	private List<Product> products;
}



Java
package com.sportyshoes.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	private String name;
	private String type; //Male Female Kid
	private int quantity;
	private double cost;
	
	@ManyToMany(cascade = CascadeType.ALL)
	private List<Saleorder> saleorders;
}



Java
package com.sportyshoes.entities;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Order {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	@ManyToOne(cascade = CascadeType.ALL)
	private Customer customer;
	
	@ManyToMany(cascade = CascadeType.ALL)
	private List<Product> products;
}



In using both these classes viz. Order and Saleorder I get error "Command Acceptance Exception" and "Error in SQL", but the difference is that when I use Saleorder then table (Saleorder) is created but when I use Order the table (Order) is not created. In both the cases other tables are created as normal.

What I have tried:

I tried searching online for this error but nothing useful came in search results. Please help me resolve these exception and help me understand why I get CommandAcceptanceException and SQLSyntaxErrorException.
Posted
Updated 25-Oct-22 0:54am
v2
Comments
Richard MacCutchan 25-Oct-22 3:48am    
The most important piece of code, the Order class, is the only one you have not shown us.
Richard MacCutchan 25-Oct-22 6:33am    
Did you correct the Orders class as I suggested?

1 solution

I would suggest going to https://www.baeldung.com/learn-jpa-hibernate[^] for some good learning resources.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900