Ich versuche, mithilfe von Hibernate und Java Spring Boot einen neuen Datensatz in meine Postgresql-Datenbank einzufügen.
Dies ist mein Benutzermodell:
Code: Select all
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
private Long id;
private String name;
private String username;
private String password;
}
Das ist mein Portfolio-Modell:
Code: Select all
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "portfolioStocks")
public class PortfolioStockEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "portfolioStock_id_seq")
private Long id;
@ManyToOne()
@JoinColumn(name = "stockId")
private StockEntity stock;
@ManyToOne()
@JoinColumn(name = "userId")
private UserEntity user;
private Number Quantity;
}
PortfolioStockRepository:
Code: Select all
@Repository
public interface PortfolioStockRepository extends CrudRepository
{
List findByUser(UserEntity userEntity);
}
PortfolioStockService:
Code: Select all
@Service
public class PortfolioStockServiceImpl implements PortfolioStockService {
....
@Override
public PortfolioStockEntity createUpdatePortfolioStock(Long id, PortfolioStockEntity portfolioStockEntity) {
portfolioStockEntity.setId(id);
return portfolioStockRepository.save(portfolioStockEntity);
}
....
}
PortfolioStockController:
Code: Select all
@RestController
public class PortfolioStockController {
....
@NoArgsConstructor
@AllArgsConstructor
private static class AddStockRequest {
public UserDto user;
public StockDto stock;
public Number quantity;
}
@PostMapping(path = "/portfolio/stock/add")
public PortfolioStockDto addPortfolioStock(@RequestBody AddStockRequest requestBody) {
UserEntity userEntity = userMapper.mapFrom(requestBody.user);
userEntity = userService.checkAuthentication(userEntity);
StockEntity stockEntity = stockMapper.mapFrom(requestBody.stock);
Optional findStockEntity = stockService.findStock(stockEntity.getSymbol());
if (findStockEntity.isEmpty())
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Stock does not exist");
else
stockEntity = findStockEntity.get();
PortfolioStockEntity portfolioStockEntity = PortfolioStockEntity.builder()
.stock(stockEntity)
.user(userEntity)
.Quantity(requestBody.quantity)
.build();
System.out.println(portfolioStockEntity);
portfolioStockEntity = portfolioStockService.createUpdatePortfolioStock(
portfolioStockEntity.getId(),
portfolioStockEntity);
return portfolioStockMapper.mapTo(portfolioStockEntity);
}
....
}
Meine Datenbank enthält bereits eine Entität von portfolioStock:
Code: Select all
{ PortfolioStockEntity(id=2, stock=StockEntity(id=3, name=Intel Corporation, symbol=InNTC, price=95.91, lastUpdated=2025-01-14), user=UserEntity(id=2, name=test, username=test, password=test), Quantity=5) }
Aber wenn die Anfrage zum Hinzufügen einer weiteren PortfolioStock-Entität an den PortfolioStockController gesendet wird, wird dieser Fehler ausgegeben:
Code: Select all
duplicate key value violates unique constraint "ukfrklsf57yxxy0h95vk4dxre5a" Detail: Key (user_id)=(2) already exists.] [insert into portfolio_stocks (quantity,stock_id,user_id,id) values (?,?,?,?)]; SQL [insert into portfolio_stocks (quantity,stock_id,user_id,id) values (?,?,?,?)]; constraint [ukfrklsf57yxxy0h95vk4dxre5a]]
PortfolioStock, der hinzugefügt werden soll:
Code: Select all
{ PortfolioStockEntity(id=null, stock=StockEntity(id=2, name=NVIDIA Corporation, symbol=NVDA, price=135.91, lastUpdated=2025-01-14), user=UserEntity(id=2, name=test, username=test, password=test), Quantity=5) }
Bearbeiten:
Sieht so aus, als ob der Fehler im Schema der Portfolio-Tabelle lag, die neu erstellt wurde Tabelle in Postgres hat den Fehler behoben.
Ich versuche, mithilfe von Hibernate und Java Spring Boot einen neuen Datensatz in meine Postgresql-Datenbank einzufügen.
Dies ist mein Benutzermodell:
[code]
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
private Long id;
private String name;
private String username;
private String password;
}
[/code]
Das ist mein Portfolio-Modell:
[code]
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "portfolioStocks")
public class PortfolioStockEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "portfolioStock_id_seq")
private Long id;
@ManyToOne()
@JoinColumn(name = "stockId")
private StockEntity stock;
@ManyToOne()
@JoinColumn(name = "userId")
private UserEntity user;
private Number Quantity;
}
[/code]
PortfolioStockRepository:
[code]
@Repository
public interface PortfolioStockRepository extends CrudRepository
{
List findByUser(UserEntity userEntity);
}
[/code]
PortfolioStockService:
[code]
@Service
public class PortfolioStockServiceImpl implements PortfolioStockService {
....
@Override
public PortfolioStockEntity createUpdatePortfolioStock(Long id, PortfolioStockEntity portfolioStockEntity) {
portfolioStockEntity.setId(id);
return portfolioStockRepository.save(portfolioStockEntity);
}
....
}
[/code]
PortfolioStockController:
[code]
@RestController
public class PortfolioStockController {
....
@NoArgsConstructor
@AllArgsConstructor
private static class AddStockRequest {
public UserDto user;
public StockDto stock;
public Number quantity;
}
@PostMapping(path = "/portfolio/stock/add")
public PortfolioStockDto addPortfolioStock(@RequestBody AddStockRequest requestBody) {
UserEntity userEntity = userMapper.mapFrom(requestBody.user);
userEntity = userService.checkAuthentication(userEntity);
StockEntity stockEntity = stockMapper.mapFrom(requestBody.stock);
Optional findStockEntity = stockService.findStock(stockEntity.getSymbol());
if (findStockEntity.isEmpty())
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Stock does not exist");
else
stockEntity = findStockEntity.get();
PortfolioStockEntity portfolioStockEntity = PortfolioStockEntity.builder()
.stock(stockEntity)
.user(userEntity)
.Quantity(requestBody.quantity)
.build();
System.out.println(portfolioStockEntity);
portfolioStockEntity = portfolioStockService.createUpdatePortfolioStock(
portfolioStockEntity.getId(),
portfolioStockEntity);
return portfolioStockMapper.mapTo(portfolioStockEntity);
}
....
}
[/code]
Meine Datenbank enthält bereits eine Entität von portfolioStock:
[code]{ PortfolioStockEntity(id=2, stock=StockEntity(id=3, name=Intel Corporation, symbol=InNTC, price=95.91, lastUpdated=2025-01-14), user=UserEntity(id=2, name=test, username=test, password=test), Quantity=5) }[/code]
Aber wenn die Anfrage zum Hinzufügen einer weiteren PortfolioStock-Entität an den PortfolioStockController gesendet wird, wird dieser Fehler ausgegeben:
[code]duplicate key value violates unique constraint "ukfrklsf57yxxy0h95vk4dxre5a" Detail: Key (user_id)=(2) already exists.] [insert into portfolio_stocks (quantity,stock_id,user_id,id) values (?,?,?,?)]; SQL [insert into portfolio_stocks (quantity,stock_id,user_id,id) values (?,?,?,?)]; constraint [ukfrklsf57yxxy0h95vk4dxre5a]][/code]
PortfolioStock, der hinzugefügt werden soll:
[code]{ PortfolioStockEntity(id=null, stock=StockEntity(id=2, name=NVIDIA Corporation, symbol=NVDA, price=135.91, lastUpdated=2025-01-14), user=UserEntity(id=2, name=test, username=test, password=test), Quantity=5) }[/code]
[b]Bearbeiten:[/b]
Sieht so aus, als ob der Fehler im Schema der Portfolio-Tabelle lag, die neu erstellt wurde Tabelle in Postgres hat den Fehler behoben.