【发布时间】:2019-09-02 11:11:05
【问题描述】:
我尝试使用 JPA 开发 Spring 服务器,但在使用多对多关系时遇到了麻烦。这些是我的实体:
@Entity
public class Item {
@Id
private Integer id;
private String name;
private Float price;
private Integer weight;
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name="itemingredient",
joinColumns = @JoinColumn(name="iditem"),
inverseJoinColumns = @JoinColumn(name="idingredient"))
private List<Ingredient> ingredients=new ArrayList<>();
public Item( String name, Float price, Integer weight, Ingredient... ingredients) {
//this.id = id;
this.name = name;
this.price = price;
this.weight = weight;
this.ingredients = Stream.of(ingredients).collect(Collectors.toList());
this.ingredients.forEach(x->x.getItems().add(this));
}
public Item() {
}
和
@Entity
public class Ingredient {
@Id
private Integer id;
private String name;
private Float proteins;
private Float lipids;
private Float carbohydrates;
private Float energeticValue;
@ManyToMany(mappedBy = "ingredients")
@JsonIgnore
private List<Item> items=new ArrayList<>();
public Ingredient(String name, Float proteins, Float lipids, Float carbohydrates, Float energeticValue) {
//this.id = id;
this.name = name;
this.proteins = proteins;
this.lipids = lipids;
this.carbohydrates = carbohydrates;
this.energeticValue = energeticValue;
}
public Ingredient(){
}
我已经创建了存储库和其余控制器。我正在为我的数据库使用 mysql。我已经创建了表,并且我有“itemingredient”表的关系。这是我的 JPA 配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "Repository")
public class JPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("model");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/nha");
dataSource.setUsername( "root" );
dataSource.setPassword( "1234" );
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty(
"hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
}
这是我的休息控制器:
@RestController
@RequestMapping(path="/items")
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping(value = "/all",method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Item>> findAll(){
return new ResponseEntity<>(itemService.findAll(),null, HttpStatus.OK);
}
在启动服务器并尝试获取所有项目后,服务器发回一个空列表。我没有正确连接到我的数据库吗?谢谢!
【问题讨论】:
-
添加Getter和setter,也试试调试
-
我有 getter 和 setter,我只是觉得在这里复制它们没什么用。我试过了,在发送列表之前它是空的
-
检查注释
@JoinTable中的表名,你应该把你的表的名字和@JoinColumn的名字一样 -
@kelgwiin 是同名
标签: java spring spring-boot jpa many-to-many