Рассмотрим следующую простую иерархию: < /p>
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Setter
@Getter
public class Brand {
@Id
Long id;
String name;
}
@Entity
@Setter
@Getter
public class BrandDetail extends Brand {
private String wpCode;
}
Предположим, что у нас есть строка в таблице бренда с идентификатором 1l и без соответствующего brand_detail row.
Следующее: < /p>
@Service
public class BrandService {
@Autowired
private BrandRepository brandRepository;
@Autowired
private BrandDetailRepository brandDetailRepository;
public void addBrandDetails(Long brandId, String wpCode) {
// Fetch the existing Brand entity
Brand brand = brandRepository.findById(brandId).orElseThrow(() -> new EntityNotFoundException("Brand not found"));
// Create a BrandDetail entity
BrandDetail brandDetail = new BrandDetail();
brandDetail.setId(brand.getId()); // Set the same ID as the existing Brand
brandDetail.setName(brand.getName()); // Copy the existing fields
brandDetail.setWpCode(wpCode); // Set the new detail
// Save the BrandDetail entity
brandDetailRepository.save(brandDetail);
}
}
< /code>
Не удается с: < /p>
Hibernate: select b1_0.id,case when b1_1.id is not null then 1 when b1_0.id is not null then 0 end,b1_0.name,b1_1.wp_code from brand b1_0 left join brand_detail b1_1 on b1_0.id=b1_1.id where b1_0.id=?
2025-01-30T16:23:23.680+01:00 TRACE 219372 --- [demo] [ main] org.hibernate.orm.jdbc.bind : binding parameter (1:BIGINT)
Подробнее здесь: https://stackoverflow.com/questions/794 ... ted-tables
Jpa inheritancetype. ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение