• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Hibernate does not allow to save data with similar id

April 10, 2020 by

Questions › Hibernate does not allow to save data with similar id
0
Vote Up
Vote Down
Garmaine asked 3 years ago

I'm learning Spring and few days ago i started learning Hibernate. I have my studying project where i need to create a shop with products. Here is entity class

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

import javax.persistence.*;

@Entity
@Table(name = "cart")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")

    private int id;
    @Column(name = "quantity")
    private int quantity;
    @Column(name = "mask")
    private String mask;
    @Column(name = "price")
    private int price;

So, i create interface

import org.springframework.data.jpa.repository.JpaRepository;

public interface CartRepository extends JpaRepository<Cart, Integer> {
}

and create controllers

@Autowired
    CartRepository cartList;

    @RequestMapping("/add-black-mask")
    public String addBlackMask() {
        cartList.save(new Cart(1, 1, "black", 3));

        return "masks/add-black-mask";
    }

    @RequestMapping("/add-build-mask")
    public String addBuildMask() {
        cartList.save(new Cart(2, 1, "build", 5));

        return "masks/add-build-mask";
    }


  @RequestMapping(value = "/save_product_to_cart")
    public ModelAndView saveProduct(@ModelAttribute(value = "cart")
                                            Cart cart, BindingResult result) {
        int index = Integer.parseInt(String.valueOf(cartList.count()));
        if (cart.getId() == 0) {
            cart.setId(index + 1);
            cartList.save(cart);
        } else {
            Cart cart1 = cartList.getOne(cart.getId());
            cart1.setMask(cart.getMask());
            cart1.setQuantity(cart.getQuantity());
            cart1.setPrice(cart.getPrice());
            cartList.save(cart1);
        }
        return new ModelAndView("redirect:/");
    }

Also, there are some other controllers for view, Thymeleaf etc, but its ok. My problem is – when i save my product 1 time – its ok, but when i save second – it didnt work( i think because i can't save 2 rows with similar ID) So it seems i have UNIQUE ID in my table and it can not be repeated. Question is – how can i delete unique id or change my code in any way? Thanks in advance!

p.s. i read some other topics here but it didnt help me.

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: hibernate, java, postgresql, spring-boot

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023