Hover the class Product and you get 'Persistent entity 'Product' should have primary key'. From the quick search I did, I didn't find anything that was related.
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Table;
#Table(name="product")
#Data
#Entity
public class Product {
}
This means your entity class should have an ID field defined based on which jpa decides what type of primary key it needs to generate.
Define a field in entity class like below from the javax.persistence package.
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
Related
Suppose you have a class with the following UUID primary key:
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
#Entity
public class MyEntity {
#Id
#GeneratedValue(generator = "UUID")
#GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
// other fields and constructor omitted for brevity
}
When you create a new MyEntity(), the id property is null. That's something I'm used to when the database is responsible for generating the id. In those cases, you must save the entity first before the id is populated. In this case the underlying database is a postgres database with id column type UUID, but the application should be able to generate the Id.
Is there a reason it wouldn't generate automatically and immediately?
Reference: https://thorben-janssen.com/generate-uuids-primary-keys-hibernate/
Scenario:
I do have JSON object as Menus.
//suppose this as
Menus menus = new Menus();
menus.setId(61);
menus.setUrl("test_url");
menus.setName("menu name");
repository.save(menus);
Current case:
It is working fine if db has menu row with id 61 as a result object gets updated.
While row with id=61 does not exists in db then this menu object gets persisted but with new id. ie. a new row is created with new auto generated ID.
Expected:
If menu where id = 61 does not exists in db then menus should be inserted in db with id=61
package com.rasello.auth.entity;
import lombok.*;
import org.hibernate.annotations.Type;
import javax.persistence.*;
#Entity
#Data
#NoArgsConstructor
#Table(name = "menus")
#Getter
#Setter
public class Menus {
#Id
#GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name;
private String url;
}
Remove this line #GeneratedValue(strategy = GenerationType.TABLE) and try again. The #GeneratedValue does what the words say. Generates value for the ID and in case you do not need that then you should not use it.
You can read more about how to set up your Ids in this article https://www.baeldung.com/hibernate-identifiers
I have an error regarding deploying the backend trough docker on localhost 8080 .
When i run the website normally (started the postgres server from inteliji) it works properly.
When i try to deploy it trough docker i get the following error:
org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bigint
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 580
The next code is an example of class using UUID
package com.ds.assignment1.ds2021_30244_rusu_vlad_assignment_1.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.UUID;
#Entity
#Data
public class Account {
#Id
#GeneratedValue(generator = "uuid4")
#GenericGenerator(name = "uuid4", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;
private String username;
private String password;
private String role;
}
The error shows that the database column is UUID whereas the JPA entity is bigint. I should mention that this may be or may not be about the ID field of this entity.
As #Adrian-klaver said you have to look at position 580 of the SQL query, which you can do by enabling hibernate query logs and looking at the last hibernate SQL query log.
I had a similar problem, spending three days to finally resolve it. My problem was due to having multiple attribute types being different than their database type. As I was migrating from Long id to UUID, it made me confuse figuring out what the cause for my error was.
I am creating a project in spring boot and as an example I am following an exercise on the internet where there is a primary key id and other fields. (the code is below).
When creating Entity / POJO class there is the #Id annotation which indicates that the member field below is the primary key of current entity with the addition of the #GeneratedValue (strategy = GenerationType.IDENTITY) annotation which is used to configure the increment of the specified column (field). And so far everything is clear.
Now a doubt arises; since the exercise I have to do does not have Id as primary key but has a code called PersonCode as primary key composed of both numeric and alphabetic characters (therefore it is not an auto-incremented field, but you have to enter it manually every time you create a new person ), what should be put in place of the #Id and #GeneratedValue annotations?
I hope I have been clear and I apologize for this question, I am a beginner.
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="student")
public class Student {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int student_id;
private String student_name;
private String student_email;
// Get and set methods
}
Since it's a manually generated value, you don't add #GeneratedValue. #Id must be used to indicate that this field represents the primary key.
Naturally, any time a Student is to be saved, it's student_id must be filled in beforehand, Spring won't generate and id for you.
I'm facing for hours with a strange proceeding in Spring Boot when try to save a mapped entity.
The entity class with a composite key that must all be set by the user is as follows:
package model
import javax.persistence.*
#Entity
#Table(name = 'MY_TABLE')
#IdClass(MyIdClass.class)
class MyClass implements Serializable{
#Id
#Column(name = "MY_COLUMN_1")
Long column1
#Id
#Column(name = "MY_COLUMN_2")
Long column2
#Id
#Column(name = "MY_COLUMN_3")
String column3
#Id
#Column(name = "MY_COLUMN_4")
Date date1
#Column(name = "MY_COLUMN_5")
Date date2
#Column(name = "MY_COLUMN_6")
BigDecimal column6
}
#Embeddable
class MyIdClass implements Serializable{
Long column1
Long column2
String column3
Date date1;
}
The corresponding repository is:
package repository
import org.springframework.data.repository.CrudRepository
interface MyRepository extends CrudRepository<MyClass, Long>{
}
My service is:
package service
import model.MyClass
import repository.MyRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
#Service
class MyService {
#Autowired
MyRepository repository
void save(MyClass myClass) {
repository.save(myClass)
}
}
My controller mounts a MyClass object with all data set, including the composite key. When it calls the service save method the object is not inserted in the database. I saw the logs and checked that there is a SELECT in MY_TABLE instead of INSERT. I tried not to inform the composite key in the object and then the save method did an INSERT with error due to null values in the primary key.
I really don't understand why the insertion is not done when the composite key has values. How can I solve it?
I've already tried with #Transactional in service class and didn't work. I didn't do any Transaction configuration in the project since Spring Boot delivers it as default.
Thanks.
It seems you are using MyIdClass as the Id for MyClass. So, the Repository should be:
interface MyRepository extends CrudRepository<MyClass, MyIdClass>{
}
Hope this help.
I take your code sample and tried it on a sample Spring Boot project, where I was able to save to H2 DB (In memory) with #Embeddable & #EmbeddedId annotations. If you would like to verify, you can clone the GitHub repo and run the BootJpaApplication.java as a Java Application.
After execution access the H2 console with the below link from local where table details can be verified.
http://localhost:8080/h2-console
https://github.com/sujittripathy/springboot-sample.git
Hope the detail helps:)