我知道这个问题已被问到several times但是,他们没有帮助我。 我有以下测试:
public class PlantCatalogTests {
@Autowired
PlantInventoryEntryRepository plantRepo;
@Test
public void queryPlantCatalog() {
assertThat(plantRepo.count(), is(14l));
}
这里是 PlantInventoryEntryRepository
@Repository
public interface PlantInventoryEntryRepository extends JpaRepository<PlantInventoryEntry, Long> {}
如您所见,此存储库基于 PlantInventoryEntry
类@Entity
@Data
public class PlantInventoryEntry {
@Id
@GeneratedValue
Long id;
@OneToOne
PurchaseOrder plant_id;
String name;
String description;
String price;
}
PurchaseOrder是另一个类,我在 PlantInventoryEntry 类中有一个实例作为属性:
@Entity
@Data
public class PurchaseOrder {
@Id
@GeneratedValue
Long id;
List<PlantReservation> reservations;
PlantInventoryEntry plant;
LocalDate issueDate;
LocalDate paymentSchedule;
@Column(precision=8,scale=2)
BigDecimal total;
@Enumerated(EnumType.STRING)
POStatus status;
LocalDate startDate;
LocalDate endDate;
}
我的主要问题是,当我运行测试时,我面临这个错误:
org.hibernate.MappingException: Could not determine type for: com.example.models.PlantInventoryEntry, at table: purchase_order, for columns: [org.hibernate.mapping.Column(plant)
我该如何解决错误?
答案 0 :(得分:2)
您需要在 PurchaseOrder 中的 PlantInventoryEntry 上使用 @ManyToOne 或 @OneToOne 注释来识别关系,取决于实体之间的实际关系。
编辑:您很可能需要确定 PlantReservation 列表与 PurchaseOrder 之间的关系,或者您需要将其标记为 @Transient ,如果它不由JPA管理。
@Entity
@Data
public class PurchaseOrder {
@Id
@GeneratedValue
Long id;
// You need to set the mappedBy attribute to the field name
// of PurchaseOrder in PlantReservation
// Update: omit mappedBy if PurchaseOrder is not mapped in PlantReservation
@OneToMany(mappedBy="order")
List<PlantReservation> reservations;
@ManyToOne
PlantInventoryEntry plant;
LocalDate issueDate;
LocalDate paymentSchedule;
@Column(precision=8,scale=2)
BigDecimal total;
@Enumerated(EnumType.STRING)
POStatus status;
LocalDate startDate;
LocalDate endDate;
}