Ganesh Patil
Ganesh Patil

Reputation: 1

@GeneratedValue not generating id value in entity class

my entity class trying to implement for stocks.java

@Entity
@Table(name="stocks")
public Class Stocks
{
@Id
@GeneratedValue
@Column(name = "stock_id")
private Integer stocked;
}

this my rest API controller I wrote stockcontroller.java

@RestController
@RequestMapping("home/stocks")
public class HomeController {

@Autowired
private StockRepo stocks;



//POST: adds a new song to the repository
@PostMapping("/add")
public void addSong(@RequestBody(required = true) Stock stock) throws DuplicateItemException {
    if(stock.existsById(stock.stockId())) {
        throw new DuplicateItemException();  
    }
    stocks.save(song);
}

}

Upvotes: 0

Views: 1141

Answers (2)

frascu
frascu

Reputation: 828

You have to define the generation strategy.

For example you can replace @GeneratedValue with:

@GeneratedValue(strategy = GenerationType.IDENTITY)

or

@GeneratedValue(strategy = GenerationType.SEQUENCE)

or

@GeneratedValue(strategy = GenerationType.TABLE)

Of course, you have to put @Transactional on the method addSong to persist the entity.

Upvotes: 2

vikas
vikas

Reputation: 68

change @GeneratedValue to @GeneratedValue(strategy = GenerationType.IDENTITY) or @GeneratedValue(strategy = GenerationType.SEQUENCE)

Upvotes: 0

Related Questions