JPA枚举查询

时间:2018-11-06 00:41:23

标签: jpa spring-data-jpa

我正在使用Spring Data JPA。我有一个.... import { SpeechToText } from 'react-native-watson'; type Props = {}; export default class App extends Component<Props> { componentWillMount(){ SpeechToText.initialize("username", "password"); } watsonTest(){ SpeechToText.startStreaming((error,text)=>{ console.log(text); console.log(error); }) } render() { return ( <View style={styles.container}> <TouchableOpacity onPress={this.watsonTest()}> <Text>Start Recording</Text> </TouchableOpacity> </View> ); } } const styles = StyleSheet.create({ container: { ..... }); 实体和一个Recipe枚举,其属性分别为DifficultyEASYMEDIUM

我的HARD类由Recipe组成,如下所示:

Difficulty

我的代码可以按预期工作,并且我能够轻松保存食谱。

我的问题从这里开始。如果我想向一个数据库列添加多个枚举怎么办?例如,所以我希望食谱可以说出其中两个困难。

1 个答案:

答案 0 :(得分:1)

使用自定义AttributeConverter

@Convert(converter = DifficultiesConverter::class)
private List<Difficulty> difficulties;


import javax.persistence.AttributeConverter
@Converter(autoApply = true)
public class DifficultiesConverter implements AttributeConverter<List<Difficulty>, String> {
  public String convertToDatabaseColumn(List<Difficulty> attribute) {
    // convert List to String, such as [EASY, MEDIUM, ...] to "EASY,MEDIUM,..."
    return ... 
  }

  public List<Difficulty> convertToEntityAttribute(String dbData) {
    // convert String to List, such as "EASY,MEDIUM,..." to [EASY, MEDIUM, ...]
    return ... 
  }
}