简体   繁体   English

mysql将datatype列设置为java set mapping

[英]mysql set datatype column to java set mapping

I having issues in mapping a mysql SET type to Java Set using JPA To illustrate my question i frame a random example below 我在使用JPA将mysql SET类型映射到Java Set时遇到问题为了说明我的问题,我在下面给出了一个随机的例子

Here is a table which has a column genre which is of type Set (ie:it will be a collection of Strings) 这是一个具有Set类型的列类型的表(即:它将是字符串的集合)

CREATE TABLE `MusicCD` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `period` ENUM('Classical', 'Modern','Antique') NOT NULL,
  `genre` SET('horror','thriller','comedy','drama','romance') ,
  PRIMARY KEY (`id`) 
  )

Below is the entity class used for the mapping 下面是用于映射的实体类

@Entity
@Table(name = "MusicCD")
class MusicCD {
private long id;
private Period period;
private Set<String> genre;

//other getter setters // 

@Column(name = "genre")
@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
public Set<String> getGenre() {
        return genre;
      }

 public void setGenre(Set<String> genre) {
      this.genre = genre;
    }
}

With this mapping there is no exception but the set is empty in the entity object because the get query sent by JPA/hibernate sents query for all fields in table MusicCD but for the genre it sends a separate query to table MusicCD_genre 使用此映射时,没有异常,但实体对象中的集合为空,因为JPA / hibernate发送的get查询会查询表MusicCD中的所有字段,但对于该类型,它会向表MusicCD_genre发送单独的查询

When i see the sql schema there is a autogenerated table MusicCD_genre which is empty. 当我看到sql架构时,有一个自动生成的表MusicCD_genre是空的。 Sending a sql select query for genre on MusicCD returns the genres. 在MusicCD上发送类型的sql select查询返回类型。 So how does the Set data type in sql work and what is the correct annotation to map it? 那么sql中的Set数据类型如何工作以及映射它的正确注释是什么?

Update: I also tried 更新:我也试过了

 @TypeDefs({@TypeDef(name = "javaSet", typeClass = HashSet.class)})

and annotate the getter with 并用。注释getter

@Type(type = "javaSet")

but this doesn't work with EOFException during de-serialization. 但是在反序列化期间,这不适用于EOFException。 This might work by replacing the HashSet with correct type to deserialize to. 这可能通过使用正确的类型替换HashSet来反序列化为。

I know it's an old question, but I prefer treat these ´MySQL special type´ columns in the getters/setters when the most use of them would be in java code. 我知道这是一个老问题,但我更喜欢在getter / setter中使用这些'MySQL特殊类型'列时最常用的是java代码。

@Entity
@Table(name = "MusicCD")
class MusicCD {
  /*...*/
  @Column(name = "genre")
  private String genreStr;
  /*...*/
  public Set<String> getGenre() {
    if(genreStr == null)
      return Collections.emptySet();
    else
      return Collections.unmodifiableSet(
        new HashSet<String>(Arrays.asList(genreStr.split(",")))
      );
  }

  public void setGenre(Set<String> genre) {
    if(genre == null)
      genreStr = null;
    else
      genreStr = String.join(",", genre);
  }
}

I use the immutable version of Set, because that avoids trying alter the set values without actually alter the DB. 我使用Set的不可变版本,因为这样可以避免尝试更改设置值而不实际更改数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM