Отношения сущностей с JoinColumnJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Отношения сущностей с JoinColumn

Сообщение Anonymous »


Hi everyone I have a problem with @JoinColumn. In my case I just want to reach Entities with @JoinColumn(Foreign Key) not using @OneToMany or @ManyToOne beacue these 2 annotation pretends to have a entity in it.

Example :

Product Entity :

@NoArgsConstructor @AllArgsConstructor @Getter @Setter @Entity @Table(name = "products") public class Product extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name", length = 50, nullable = false) private String name; @Column(name = "price", length = 10, nullable = false) private Double price; @Column(name = "expiration_date", nullable = false) private LocalDateTime expirationDate; @Column(name = "description", nullable = false) private String description; @Enumerated(EnumType.STRING) @Column(name = "product_status", length = 30, nullable = false) private ProductStatus productStatus = ProductStatus.ACTIVE; @JoinColumn(name = "categoryId") private Long categoryId; } Category Entity :

@NoArgsConstructor @AllArgsConstructor @Getter @Setter @Entity @Table(name = "categories") public class Category extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name", length = 50, nullable = false) private String name; } As can be seen I use categoryId in ProductEntity with type Long as Foreign Key with @JoinColumn annotation.

At my saveProduct(POST) process I just enter categoryId for saveProduct.

{ "name": "Fruit", "price": 13.40, "expirationDate": "2024-04-07T00:05:49.713Z", "description": "Apple", "categoryId": 5 } And my saveCategory body just use String name

{ "name": "Fruit" } Before my save operations I am using DTO and Requests with mapstruct to convert Request to Entity after that convert to Entity to DTO

ProductMapper :

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface ProductMapper { ProductMapper INSTANCE = Mappers.getMapper(ProductMapper.class); Product convertToProduct(ProductSaveRequest request); ProductDTO convertToProductDTO(Product product); @Mapping(target = "id", ignore = true) void updateProductFields(@MappingTarget Product product, ProductUpdateRequest request); } Category Mapper :

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface CategoryMapper { CategoryMapper INSTANCE = Mappers.getMapper(CategoryMapper.class); Category convertToCategory(CategorySaveRequest request); CategoryDTO convertToCategoryDTO(Category category); @Mapping(target = "id", ignore = true) void updateCategoryFields(@MappingTarget Category category, CategoryUpdateRequest request); } ProductDTO :

public record ProductDTO(Long id, String name, Double price, LocalDateTime expirationDate, String description, ProductStatus productStatus, Long categoryId) { } CategoryDTO :

public record CategoryDTO(Long id, String name) { } ProductSaveRequest :

public record ProductSaveRequest(String name, Double price, LocalDateTime expirationDate, String description, Long categoryId) { } CategorySaveRequest :

public record CategorySaveRequest(String name) { } Problem Notice :

When I save Category I just give the name not Product or ProductList or ProductId because it doesn't make no sense for case.

Well all of the end my problem is I have a get request(GET) with named getProductsWithCategoryId It should return products with categoryId{id} as a list. How can I reach or use JoinColumn in Category or should I ? or when I use JoinColumn in Category Entity with private Long productId; how can I reach products in categoryService and return a List ?


Источник: https://stackoverflow.com/questions/780 ... joincolumn
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»