关系返回映射通过引用未知目标实体属性

时间:2019-06-19 17:27:41

标签: hibernate spring-boot jpa

我对Meal和MealGroup之间的关系有疑问。 Hibernate回报我:“ mappedBy通过引用未知目标实体属性”。 我有两个实体:

我尝试删除@Join Column和maptedBy。已创建其他表,但我想加入列。

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

  private static final Logger logger = LoggerFactory.getLogger(CacheConfig.class);

  @Autowired
  private MyCacheProperties myCacheProperties;

  @Bean
  @Override
  public CacheManager cacheManager() {
    // create a custom configured cache for each of the customCacheSpecs in the myCacheProperties
    final List<CaffeineCache> customCaches = myCacheProperties.getCustomCacheSpecs().entrySet().stream()
        .map(entry -> buildCache(entry.getKey(), entry.getValue()))
        .collect(Collectors.toList());
    // put the custom caches in a SimpleCacheManager
    final SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
    simpleCacheManager.setCaches(customCaches);

    // create a Caffeine Cache manager based on the defaultCacheSpec in the myCacheProperties
    final CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCacheSpecification(myCacheProperties.getDefaultCacheSpec());
    caffeineCacheManager.setAllowNullValues(false);

    // create a CompositeCacheManager which will first look for a customized cache from the simpleCacheManager and then
    // if no cache is found it will delegate to the caffeineCacheManager.  If the caffeineCacheManager already has
    // created an appropriate cache it will be used, other wise it will create a new cache with the default
    // settings
    final CompositeCacheManager compositeCacheManager = new CompositeCacheManager(simpleCacheManager,
        caffeineCacheManager);
    return compositeCacheManager;
  }

  private CaffeineCache buildCache(final String name, final String cacheSpec) {
    final CaffeineCache caffeineCache = new CaffeineCache(name, Caffeine.from(cacheSpec)
        .build());
    logger.debug("created custom cache: name='{}', and spec='{}'", name, cacheSpec);
    return caffeineCache;
  }
}

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class Meal {

    @Id
    @NotNull
    private int Id;

    private String name;

    @OneToMany(mappedBy = "meal")
    private List<Product> products;

    @ManyToOne
    @JoinColumn(name = "meal_group_id")
    private MealGroup mealGroup;
}

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要在mappedBy中使用类成员名称。

//MealGroup class
...
@OneToMany(mappedBy = "mealGroup")
private List<Meal> meals;