I'm currently learning Spring-Boot and Spring-Data-JPA.
I'm using a postgresql database for storing the data.
My goal is to store ingredients with a unique and custom ID (you just type it in when creating it), but when another ingredient with the same ID gets inserted, there should be some kind of error. In my understanding, this is what happens when I use the #Id annotation, hibernate also logs the correct create table statement.
This is my Ingredient class:
public class Ingredient {
#Id
#Column(name = "ingredient_id")
private String ingredient_id;
#Column(name = "name")
private String name;
#Column(name = "curr_stock")
private double curr_stock;
#Column(name = "opt_stock")
private double opt_stock;
#Column(name = "unit")
private String unit;
#Column(name = "price_per_unit")
private double price_per_unit;
#Column(name = "supplier")
private String supplier;
-- ... getters, setters, constructors (they work fine, I can insert and get the data)
}
My controller looks like this:
#RestController
#RequestMapping(path = "api/v1/ingredient")
public class IngredientController {
private final IngredientService ingredientService;
#Autowired
public IngredientController(IngredientService ingredientService) {
this.ingredientService = ingredientService;
}
#GetMapping
public List<Ingredient> getIngredients(){
return ingredientService.getIngredients();
}
#PostMapping
public void registerNewStudent(#RequestBody Ingredient ingredient) {
ingredientService.saveIngredient(ingredient);
}
}
And my service class just uses the save() method from the JpaRepository to store new ingredients.
To this point I had the feeling, that I understood the whole thing, but when sending two post-requests to my application, each one containing an ingredient with the id "1234", and then showing all ingredients with a get request, the first ingredient just got replaced by the second one and there was no error or smth. like that in between.
Sending direct sql insert statements to the database with the same values throws an error, because the primary key constraint gets violated, just as it should be. Exactly this should have happened after the second post request (in my understanding).
What did I get wrong?
Update:
From the terminal output and the answers I got below, it is now clear, that the save() method can be understood as "insert or update if primary key is already existing".
But is there a better way around this than just error-handle every time when saving a new entry by hand?
The save method will create or update the entry if the id already exists. I'd switch to auto generating the ID when inserting, instead of manually creating the IDs. That would prevent the issue you have
When saving a new ingredient, jpa will perform an update if the value contained in the “id” field is already in the table.
A nice way through which you can achieve what you want is
ingredientRepository.findById(ingredientDTO.getIngredientId()).
ifPresentOrElse( ingredientEntity-> ResponseEntity.badRequest().build(), () -> ingredientRepository.save(ingredientDTO));
You can return an error if the entity is already in the table otherwise (empty lambda), you can save the new row
This is a downside to using CrudRepository save() on an entity where the id is set by the application.
Under the hood EntityManager.persist() will only be called if the id is null otherwise EntityManager.merge() is called.
Using the EntityManager directly gives you more fine grained control and you can call the persist method in your application when required
I'm using H2 Database with hibernate on JAVA and I'm getting a weird error.
I have created my abstract repository to manage the basic CRUD operation.
The exception I am getting is this:
java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl#d20d74a is closed
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:135)
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:254)
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:116)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:294)
at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:139)
at repositories.AbstractRepository.save(AbstractRepository.java:32)
at services.ResultService.saveResult(ResultService.java:76)
at services.API.WebRequestService.run(WebRequestService.java:124)
at services.API.ThreadService.run(ThreadService.java:67)
AbstractRepository save method:
public <T> T save(T t) {
Transaction transaction = null;
try (Session session = HibernateConfig.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
Serializable entityId = session.save(t);
transaction.commit();
T createdEntity = (T) session.get(t.getClass(), entityId);
return createdEntity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
return null;
}
I am a CS student and I am not very much familiar with Hibernate. I'm not getting this error on my computer, only on other computers with the JAR file builded.
P.S English isn't my main language so I am very sorry if you don't understand me clearly!
After hours of debugging I found the error!
The error was that a column exceeded the length and the exception was coming from the catch block.
The catch block was trying to rollback something that its connection was already closed.
I hope this will be helpful to someone!
I got the same error when trying to create an embedded relationship between two tables using Hibernate version 5.5.3. Yes the above answer was helpful for me to debug the error in a single go. Thanks to #William. It was same in my case too, the catch block was trying to rollback the transaction due to an exception occurred in the Embeddable class. The issue was that I did not have a default constructor inside Embeddable class.
Thanks!
#Vikarm's answer and #William's answer pointed me to the right direction. You simply need to create your composite ID in the constructor of the class that has the EmbeddedId.
Here's how it looks like in code (full example is included for completeness purposes)
#Entity(name = "artwork_rating")
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
public class ArtworkRating {
#EmbeddedId
private ArtworkRatingKey id; // This needs to be instantiated*
private int score;
private String content;
// --------- Relations --------- //
#ManyToOne
#MapsId("userId")
#JoinColumn(name = "user_id")
User user;
#ManyToOne
#MapsId("artworkId")
#JoinColumn(name = "artwork_id")
Artwork artwork;
// --------- Constructors --------- //
public ArtworkRating(int score, String content, User user, Artwork artwork) {
this.id = new ArtworkRatingKey(user.getId(), artwork.getId()); // *as shown here
this.score = score;
this.content = content;
this.user = user;
this.artwork = artwork;
}
}
I have 2 POJO classes in Java: Phrase and Tag, in a many-to-many relationship:
Phrase.java
#Entity
#EntityListeners(value={PhraseListener.class})
public class Phrase {
#Id
#GeneratedValue
#Column(name="id")
private Long phraseId;
#Column(nullable=false)
private String text;
#ManyToMany(cascade=CascadeType.ALL)
#JoinTable(name="phrase_has_tag",
joinColumns={#JoinColumn(name="phrase_id",referencedColumnName="id")},
inverseJoinColumns={#JoinColumn(name="tag_uname",referencedColumnName="uname")})
private Collection<Tag> tagObjects;
#Transient
private Set<String> tags;
public Phrase() {
tagObjects = new ArrayList<Tag>();
tags = new HashSet<String>();
}
// getters and setters
// …
public void addTagObject(Tag t) {
if (!getTagObjects().contains(t)) {
getTagObjects().add(t);
}
if (!t.getPhrases().contains(this)) {
t.getPhrases().add(this);
}
}
public void addTag(String tagName) {
if (!getTags().contains(tagName)) {
getTags().add(tagName);
}
}
Tag.java
#Entity
public class Tag {
#Id
#Column(name="uname")
private String uniqueName;
private String description;
#ManyToMany(mappedBy="tagObjects")
private Collection<Phrase> phrases;
public Tag() {
phrases = new ArrayList<Phrase>();
}
// getters and setters
// …
The primary key for the tag entity is its name. I want to keep in Phrase.java a Set of tag names "synchronized" with the tagObjects field of the many-to-many relationship, and viceversa. For doing this, I add a listener to Phrase.java:
public class PhraseListener {
#PostLoad
public void postLoad(Phrase p) {
System.out.println("In post load");
for (Tag tag : p.getTagObjects()) {
p.addTag(tag.getUniqueName());
}
}
#PrePersist
public void prePersist(Phrase p) {
System.out.println("In pre persist");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestJPA");
EntityManager em = emf.createEntityManager();
for (String tagName : p.getTags()) {
Tag t = em.find(Tag.class, tagName);
if (t == null) t = new Tag(tagName);
p.addTagObject(t);
}
}
}
which after loading, it creates the set of tag names from the tag objects and before persisting it reads the set of tag names, and fetch or create tag objects.
My problem is that if I try to create multiple phrases which share tags, JPA instead of only creating the relationship (insert into the join table) it also create tag objects which violate primary key constraint.
transaction.begin();
Phrase p = new Phrase("Never ask what sort of computer a guy drives. If he's a Mac user, he'll tell you. If not, why embarrass him?", "Tom Clancy");
p.addTag("apple");
p.addTag("macintosh");
em.persist(p);
transaction.commit();
transaction.begin();
p = new Phrase("It's better to be a pirate than to join the Navy.", "Steve Jobs");
p.addTag("apple");
em.persist(p);
transaction.commit();
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: [SQLITE_CONSTRAINT] Abort due to constraint violation (column uname is not unique)
Error Code: 0
Call: INSERT INTO TAG (uname, DESCRIPTION) VALUES (?, ?)
bind => [apple, null]
You haven't shown your addTag method in Phrase, but I assume that you have somewhere in there an expression new Tag() and it would look somehow similar to this:
public void addTag(String tagName) {
Tag tag = new Tag();
tag.setUniqueName(tagName);
tag.getPhrases().add(this);
this.tagObjects.add(tag);
}
In this case the method addTag will create new object of type Tag everytime the method is called, which will result in different entries in the relational table, cause Hibernate persists the whole objects, not only particular fields of theirs, regardless if these fields are primary keys or not.
After calling the method addTag two times, you will create two different objects and Hibernate could not know if those two objects relate to the same entry in the DB or not. This means that even though they have the same uniqueName, they could have a different description.
Imagine the following scenario:
transaction.begin();
Phrase p = new Phrase("Never ask what sort of computer a guy drives. If he's a Mac user, he'll tell you. If not, why embarrass him?", "Tom Clancy");
Tag t = new Tag();
t.setUniqueName("apple");
t.setDescription("This is an example apple");
p.getTagObjects().add(t);
em.persist(p);
transaction.commit();
transaction.begin();
p = new Phrase("It's better to be a pirate than to join the Navy.", "Steve Jobs");
t = new Tag();
t.setUniqueName("apple");
t.setDescription("Another description of the apple");
em.persist(p);
transaction.commit();
With this example the difference should be more obvious and should illustrate why it is impossible to Hibernate to know when are you referring to the same entry in the DB with two or more different objects.
As a solution I would suggest you to change the method addTag so that it has the following signature public void addTag(Tag tag) {... and keep track of the existing tags somewhere centralized or you can try out em.merge(p); instead of em.persist(p);
Well the question pretty much says everything. Using JPARepository how do I update an entity?
JPARepository has only a save method, which does not tell me if it's create or update actually. For example, I insert a simple Object to the database User, which has three fields: firstname, lastname and age:
#Entity
public class User {
private String firstname;
private String lastname;
//Setters and getters for age omitted, but they are the same as with firstname and lastname.
private int age;
#Column
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
#Column
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
private long userId;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public long getUserId(){
return this.userId;
}
public void setUserId(long userId){
this.userId = userId;
}
}
Then I simply call save(), which at this point is actually an insert into database:
User user1 = new User();
user1.setFirstname("john"); user1.setLastname("dew");
user1.setAge(16);
userService.saveUser(user1);// This call is actually using the JPARepository: userRepository.save(user);
So far so good. Now I want to update this user, say change his age. For this purpose I could use a Query, either QueryDSL or NamedQuery, whatever. But, considering I just want to use spring-data-jpa and the JPARepository, how do I tell it that instead of an insert I want to do an update?
Specifically, how do I tell spring-data-jpa that users with the same username and firstname are actually EQUAL and that the existing entity supposed to be updated? Overriding equals did not solve this problem.
Identity of entities is defined by their primary keys. Since firstname and lastname are not parts of the primary key, you cannot tell JPA to treat Users with the same firstnames and lastnames as equal if they have different userIds.
So, if you want to update a User identified by its firstname and lastname, you need to find that User by a query, and then change appropriate fields of the object your found. These changes will be flushed to the database automatically at the end of transaction, so that you don't need to do anything to save these changes explicitly.
EDIT:
Perhaps I should elaborate on overall semantics of JPA. There are two main approaches to design of persistence APIs:
insert/update approach. When you need to modify the database you should call methods of persistence API explicitly: you call insert to insert an object, or update to save new state of the object to the database.
Unit of Work approach. In this case you have a set of objects managed by persistence library. All changes you make to these objects will be flushed to the database automatically at the end of Unit of Work (i.e. at the end of the current transaction in typical case). When you need to insert new record to the database, you make the corresponding object managed. Managed objects are identified by their primary keys, so that if you make an object with predefined primary key managed, it will be associated with the database record of the same id, and state of this object will be propagated to that record automatically.
JPA follows the latter approach. save() in Spring Data JPA is backed by merge() in plain JPA, therefore it makes your entity managed as described above. It means that calling save() on an object with predefined id will update the corresponding database record rather than insert a new one, and also explains why save() is not called create().
Since the answer by #axtavt focuses on JPA not spring-data-jpa
To update an entity by querying then saving is not efficient because it requires two queries and possibly the query can be quite expensive since it may join other tables and load any collections that have fetchType=FetchType.EAGER
Spring-data-jpa supports update operation.
You have to define the method in Repository interface.and annotated it with #Query and #Modifying.
#Modifying
#Query("update User u set u.firstname = ?1, u.lastname = ?2 where u.id = ?3")
void setUserInfoById(String firstname, String lastname, Integer userId);
#Query is for defining custom query and #Modifying is for telling spring-data-jpa that this query is an update operation and it requires executeUpdate() not executeQuery().
You can specify the return type as int, having the number of records being updated.
Note: Run this code in a Transaction.
You can simply use this function with save() JPAfunction, but the object sent as parameter must contain an existing id in the database otherwise it will not work, because save() when we send an object without id, it adds directly a row in database, but if we send an object with an existing id, it changes the columns already found in the database.
public void updateUser(Userinfos u) {
User userFromDb = userRepository.findById(u.getid());
// crush the variables of the object found
userFromDb.setFirstname("john");
userFromDb.setLastname("dew");
userFromDb.setAge(16);
userRepository.save(userFromDb);
}
As what has already mentioned by others, the save() itself contains both create and update operation.
I just want to add supplement about what behind the save() method.
Firstly, let's see the extend/implement hierarchy of the CrudRepository<T,ID>,
Ok, let's check the save() implementation at SimpleJpaRepository<T, ID>,
#Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
As you can see, it will check whether the ID is existed or not firstly, if the entity is already there, only update will happen by merge(entity) method and if else, a new record is inserted by persist(entity) method.
spring data save() method will help you to perform both: adding new item and updating an existed item.
Just call the save() and enjoy the life :))
Using spring-data-jpa save(), I was having same problem as #DtechNet. I mean every save() was creating new object instead of update. To solve this I had to add version field to entity and related table.
This is how I solved the problem:
User inbound = ...
User existing = userRepository.findByFirstname(inbound.getFirstname());
if(existing != null) inbound.setId(existing.getId());
userRepository.save(inbound);
With java 8 you can use repository's findById in UserService
#Service
public class UserServiceImpl {
private final UserRepository repository;
public UserServiceImpl(UserRepository repository) {
this.repository = repository;
}
#Transactional
public void update(User user) {
repository
.findById(user.getId()) // returns Optional<User>
.ifPresent(user1 -> {
user1.setFirstname(user.getFirstname);
user1.setLastname(user.getLastname);
repository.save(user1);
});
}
}
public void updateLaserDataByHumanId(String replacement, String humanId) {
List<LaserData> laserDataByHumanId = laserDataRepository.findByHumanId(humanId);
laserDataByHumanId.stream()
.map(en -> en.setHumanId(replacement))
.collect(Collectors.toList())
.forEach(en -> laserDataRepository.save(en));
}
Specifically how do I tell spring-data-jpa that users that have the
same username and firstname are actually EQUAL and that it is supposed
to update the entity. Overriding equals did not work.
For this particular purpose one can introduce a composite key like this:
CREATE TABLE IF NOT EXISTS `test`.`user` (
`username` VARCHAR(45) NOT NULL,
`firstname` VARCHAR(45) NOT NULL,
`description` VARCHAR(45) NOT NULL,
PRIMARY KEY (`username`, `firstname`))
Mapping:
#Embeddable
public class UserKey implements Serializable {
protected String username;
protected String firstname;
public UserKey() {}
public UserKey(String username, String firstname) {
this.username = username;
this.firstname = firstname;
}
// equals, hashCode
}
Here is how to use it:
#Entity
public class UserEntity implements Serializable {
#EmbeddedId
private UserKey primaryKey;
private String description;
//...
}
JpaRepository would look like this:
public interface UserEntityRepository extends JpaRepository<UserEntity, UserKey>
Then, you could use the following idiom: accept DTO with user info, extract name and firstname and create UserKey, then create a UserEntity with this composite key and then invoke Spring Data save() which should sort everything out for you.
As mentioned by others answer, method save() is dual function. It can both do save or update, it's automatically update if you provide the id.
for update method in controller class I suggested to use #PatchMapping. below is the example.
#Save method POST
{
"username": "jhon.doe",
"displayName": "Jhon",
"password": "xxxyyyzzz",
"email": "jhon.doe#mail.com"
}
#PostMapping("/user")
public void setUser(#RequestBody User user) {
userService.save(user);
}
#Update method PATCH
{
"id": 1, // this is important. Widly important
"username": "jhon.doe",
"displayName": "Jhon",
"password": "xxxyyyzzz",
"email": "jhon.doe#mail.com"
}
#PatchMapping("/user")
public void patchUser(#RequestBody User user) {
userService.save(user);
}
Maybe you're wondering where the id's come from. It comes from the database of course, you want to update the existing data right?
If your primary key is autoincrement then, you have to set the value for the primary key.
for the save(); method to work as a update().else it will create a new record in db.
if you are using jsp form then use hidden filed to set primary key.
Jsp:
<form:input type="hidden" path="id" value="${user.id}"/>
Java:
#PostMapping("/update")
public String updateUser(#ModelAttribute User user) {
repo.save(user);
return "redirect:userlist";
}
also look at this:
#Override
#Transactional
public Customer save(Customer customer) {
// Is new?
if (customer.getId() == null) {
em.persist(customer);
return customer;
} else {
return em.merge(customer);
}
}
Use #DynamicUpdate annotation. it is cleaner and you don't have to deal with querying the database in order to get the saved values.
You can see the example below:
private void updateDeliveryStatusOfEvent(Integer eventId, int deliveryStatus) {
try {
LOGGER.info("NOTIFICATION_EVENT updating with event id:{}", eventId);
Optional<Event> eventOptional = eventRepository.findById(eventId);
if (!eventOptional.isPresent()) {
LOGGER.info("Didn't find any updatable notification event with this eventId:{}", eventId);
}
Event event = eventOptional.get();
event.setDeliveryStatus(deliveryStatus);
event = eventRepository.save(event);
if (!Objects.isNull(event)) {
LOGGER.info("NOTIFICATION_EVENT Successfully Updated with this id:{}", eventId);
}
} catch (Exception e) {
LOGGER.error("Error :{} while updating NOTIFICATION_EVENT of event Id:{}", e, eventId);
}
}
Or Update Using Native Query:
public interface YourRepositoryName extends JpaRepository<Event,Integer>{
#Transactional
#Modifying
#Query(value="update Event u set u.deliveryStatus = :deliveryStatus where u.eventId = :eventId", nativeQuery = true)
void setUserInfoById(#Param("deliveryStatus")String deliveryStatus, #Param("eventId")Integer eventId);
}
I did this for my Entity UserModel:
In the Controller:
#PutMapping("/{id}")
public Optional<UserModel> update(#RequestBody UserModel user, #PathVariable Long id) {
return this.userService.update(user, id);
}
And in the Service:
public Optional<UserModel> update(UserModel req, Long id){
Optional<UserModel> user = userRepository.findById(id);
if (user != null) {
userRepository.save(req);
}
return user;
}
Example with postman:
Postman method PUT example
I have what I think is a common scenario in JDO. I have a simple persistent class, say
#PersistenceCapable
public class Person {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
private long id;
#Persistent
#Unique
private String name
//constructor, equals, hashcode, etc...
}
Then I want to do a simple add, but throw a custom exception if the add fails because the unique constraint was violated
public void addPerson(String name) throws NotUniqueException {
PersistenceManager pm = getPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person toAdd = new Person(name);
pm.makePersistent(toAdd);
// how do I throw a NotUniqueException if the transaction fails due to
// "name" not being unique?
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
How do I detect this error condition in JDO? I'm fine with having to do a query first to check whether it will succeed, as long as the check and add operation are atomic. I'm just new to JDO transactions and am not sure what guarantees I get if I do the query and checking in the transaction.
According to this answer, with JPA I would have to dig through the exception chain for the vender-specific cause exception and use that information. Is that true of JDO?
JDODataStoreException with nested datastore-specific exception, as per the JDO spec