I can't create table order because table name match sql syntax. How can I resolve it? Is there any property in application.properties support it?
#Data
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "order")
public class Order {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(nullable = false)
private Double productPrice;
#Column(nullable = false)
private Double shipPrice;
#Column(nullable = false)
private Integer status;
}
Use
#Table(name="\"order\"")
Related
Hello guys i don't know why but when i tried to define an association
#one to many i get a null in the service_id and question_id.
first entity
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
#Inheritance(strategy = InheritanceType.JOINED)
public class Services implements Serializable{
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
#Column(nullable = false, updatable = false)
private Long id;
private String serviceName;
private String description;
private String image;
#OneToMany(cascade = CascadeType.ALL)
private List<Questions> questions;
Second entity
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Questions implements Serializable {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
#Column(nullable = false, updatable = false)
private Long id;
private String question;
#Column(name="service_id")
private String service_id;
#OneToMany(cascade = CascadeType.ALL)
private List<Answers> answers;
third entity
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Answers implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(nullable = false, updatable = false)
private Long id;
private String answer;
private double cost;
#Column(name="question_id")
private String question_id;
you can find the image for the tables bellow
enter image description here
All you need to do is remove the following line from Both of your models:
#Column(nullable = false, updatable = false)
After this line: #GeneratedValue(strategy = GenerationType.IDENTITY)
That will solve your problem.
Class Jobs has Many to One relationship with Profile.
When I retrieve through profileRepository.getByProfileId(id) the response returns recursive data.
Also if you notice Profile has Login object. I don't want to return that as well.
#Entity
#Table(name = "tbl_profile")
#Builder(toBuilder = true)
#NoArgsConstructor
#AllArgsConstructor
#Getter
public class Profile {
#Id
#Column(name = "profile_id")
#GeneratedValue(strategy = GenerationType.AUTO)
long profileId;
#NonNull
#Column(name = "name")
String name;
#Column(name = "description", nullable = false)
String description;
#OneToOne
#JoinColumn(name = "login_id",
referencedColumnName = "login_id")
Login login;
#OneToMany(
mappedBy = "profile"
)
List<Jobs> job;
Class Jobs
#Entity
#Table(name = "tbl_job")
#Builder(toBuilder = true)
#NoArgsConstructor
#AllArgsConstructor
#Getter
public class Jobs {
#Id
#Column(name = "job_id")
#GeneratedValue(strategy = GenerationType.AUTO)
long jobId;
#NonNull
#Column(name = "job_role", nullable = false)
String joRole;
#Column(name = "description", nullable = false)
String description;
#ManyToOne
#JoinColumn(name = "profile_id",
referencedColumnName = "profile_id")
Profile profile;
}
Use #JsonIgnore to the property to ignore the output on JSON. Also according to your business logic, recheck if you need bidirectional association. You could maybe add only unidirectional association.
I'm struggling to join two entity models using jpa crudRepository interface. i can't figure out how to map two entity model and write query inside #Query annotation. these are my entity classes.
I want to execute this query "SELECT dppd.payment_plan_id,dppd.attribute_value,dppd.attribute_id FROM taxi_driver_mapping AS tdm JOIN driver_payment_plan_details AS dppd on dppd.payment_plan"
#Getter
#Setter
#Entity
#ToString
#Table(name = "driver_payment_plan_details")
#NoArgsConstructor
public class DriverPaymentPlanDetails
{
#Id
#Column(name = "id")
private int id ;
#Column(name = "payment_plan_id")
private long paymentPlanId;
#Column(name = "attribute_id")
private int attributeId;
#Column(name = "attribute_value")
private float attributeValue;
}
#Getter
#Setter
#ToString
#Table(name = "taxi_driver_mapping")
#Entity
#NoArgsConstructor
public class TaxiDriverMapping
{
#Column(name = "mapping_id")
#Id
private Long mappingId;
#Column(name = "mapping_driverid")
private Long mappingDriverId;
#Column(name = "mapping_taxi_model_id")
private String mappingTaxiModelId;
#Column(name = "mapping_status")
private String mappingStatus;
#Column(name = "mapping_payment_plan_id")
private Long mappingPaymentPlanId;
}
thank you guys for showing a correct path, this is my code.
#Getter
#Setter
#ToString
#Table(name = "taxi_driver_mapping")
#Entity
#NoArgsConstructor
public class TaxiDriverMapping
{
#Column(name = "mapping_id")
#Id
private Long mappingId;
#Column(name = "mapping_driverid")
private Long mappingDriverId;
#Column(name = "mapping_taxi_model_id")
private String mappingTaxiModelId;
#Column(name = "mapping_status")
private String mappingStatus;
#Column(name = "mapping_payment_plan_id")
private Long mappingPaymentPlanId;
#OneToMany(mappedBy = "taxiDriverMapping")
private List<DriverPaymentPlanDetails> driverPaymentPlanDetails;
}
#Getter
#Setter
#Entity
#ToString
#Table(name = "driver_payment_plan_details")
#NoArgsConstructor
public class DriverPaymentPlanDetails
{
#Id
#Column(name = "id")
private int id ;
#Column(name = "payment_plan_id")
private long paymentPlanId;
#Column(name = "attribute_id")
private int attributeId;
#Column(name = "attribute_value")
private float attributeValue;
#ManyToOne()
#JoinColumn(name="payment_plan_id",insertable = false,updatable = false)
private TaxiDriverMapping taxiDriverMapping;
}
I just do not want to do:
myEntity = findById(Long id)
findAllByEntityAnd*(MyEntity myEntity, *)
instead I want do:
findAllByEntityAnd*(Long entityId, *)
Is there something I missed to achieve what I wanted or I just cannot achieve it directly?
Thanks for the help ~
When I tried it, the Spring prompted me:
java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.util.List com.worksap.morphling.raptor.dump.thread.dao.ThreadDoRepository.findAllByDumpDoAndLocksWaitingContains(java.lang.Long,java.lang.String)!
Here is my table for your reference:
#Data
#Builder
#Entity
#Table(name = "thread_info")
#AllArgsConstructor
#NoArgsConstructor
public class ThreadDo implements Serializable {
private static final long serialVersionUID = -1L;
String name;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne
#JoinColumn(name = "thread_dump_id")
#JsonIgnore
private ThreadDumpDo dumpDo;
...
}
#Data
#Builder
#Entity
#Table(name = "thread_dump")
#AllArgsConstructor
#NoArgsConstructor
public class ThreadDumpDo implements Serializable {
private static final long serialVersionUID = -1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(
mappedBy = "dumpDo",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<ThreadDo> threadDoList;
}
And then I have a ThreadDoRepository and want to locate the threadDos directly like this:
List<ThreadDo> findAllByDumpDoAndLocksWaitingContains(Long dumpId, String lockWaiting);
I can achieve it via #Query as follows, but I really think it's pretty ugly since it's easy to interpret (I think).
#Query(value = "select * from thread_info t where t.thread_dump_id = ?1 and t.locks_waiting like ?2",
nativeQuery = true)
List<ThreadDo> findAllByDumpDoAndLocksWaitingContains(Long dumpId, String lockWaiting);
Try this
List<ThreadDo> findAllByDumpDo_IdAndLocksWaitingContains(Long dumpId, String lockWaiting);
I have an unidirectional OneToMany JPA entity mapping in my (Spring Framework + Spring Data + Hibernate JPA) project. Entity classes are like in the following code.(I have removed irrelevant class members for brevity).
#Entity
#Table(name = "employees")
class Employee{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "employee_id")
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
#Entity
#Table(name = "department_assignments")
class DepartmentAssignment{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#NotNull
#Column(name = "employee_id")
private Integer employeeId;
#NotNull
#Column(name = "department_id")
private Integer departmentId;
}
#Entity
#Table(name = "departments")
class Department{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
}
And, in one of my service classes have a method to remove a DepartmentAssignment from an Employee like below.
public Employee deleteDepartmentAssignment(Integer empId, Integer deptAssignmentId) {
Employee employee = employeeRepository.findOne(empId);
if(employee != null) {
for ( DepartmentAssignment da : employee.getDepartmentAssignments()) {
if(da.getId().equals(deptAssignmentId)) {
employee.getDepartmentAssignments().remove(da);
employee = employeeRepository.save(employee);
break;
}
}
}
return employee;
}
However, calling above methods gives me an error: org.hibernate.exception.ConstraintViolationException ,and in the SQL log, I can see Column 'employee_id' cannot be null error for the last SQL statement of the transaction.
Can anybody tell me what I'm doing wrong here and how to get it fixed?
You don't need to add
#NotNull
#Column(name = "employee_id")
private Integer employeeId;
to the Employee, if you use #JoinColumn(name = "employee_id"). Try to remove it.
You can try the following, not sure why you use the plain id in the object. Thats not object relational mapping.
For more details see Hibernate triggering constraint violations using orphanRemoval
#Entity
#Table(name = "employees")
class Employee{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = true)
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
#Entity
#Table(name = "department_assignments")
class DepartmentAssignment{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ManyToOne(optional=false)
private Employee employee;
#ManyToOne(optional=false)
private Department department;
}
#Entity
#Table(name = "departments")
class Department{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
You must look .hbm.xml file and you should mapping your Entity in this file and
you can look this example
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
I hope it will be useful for you.
try removing
cascade = CascadeType.ALL
but im not 100% sure..