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;
}
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;
}
Code: Select all
@Repository
public interface PortfolioStockRepository extends CrudRepository
{
List findByUser(UserEntity userEntity);
}
Code: Select all
@Service
public class PortfolioStockServiceImpl implements PortfolioStockService {
....
@Override
public PortfolioStockEntity createUpdatePortfolioStock(Long id, PortfolioStockEntity portfolioStockEntity) {
portfolioStockEntity.setId(id);
return portfolioStockRepository.save(portfolioStockEntity);
}
....
}
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);
}
....
}
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) }
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]]
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) }
Sieht so aus, als ob der Fehler im Schema der Portfolio-Tabelle lag, die neu erstellt wurde Tabelle in Postgres hat den Fehler behoben.