我知道已经问过这个问题,但他们没有帮助我。
我有以下测试:
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)
我该如何解决错误?
最佳答案
您需要通过在PurchaseOrder中的PlantInventoryEntry上使用@ManyToOne或@OneToOne注释来确定关系,具体取决于实体之间的实际关系是什么。
编辑:您很可能需要识别PlantReservations列表和PurchaseOrder之间的关系,或者如果它不是由JPA管理的,则需要将其标记为@Transient。
@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;
}