Prøvede dette, men forstod ingenting!
At arbejde fuldt ud med jsonb
i Spring Data JPA (Hibernate) projekt med Vlad Mihalcea's hibernate-types lib, du skal bare gøre følgende:
1) Tilføj dette bibliotek til dit projekt:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.2.2</version>
</dependency>
2) Brug derefter dens typer i dine entiteter, for eksempel:
@Data
@NoArgsConstructor
@Entity
@Table(name = "parents")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = SEQUENCE)
private Integer id;
@Column(length = 32, nullable = false)
private String name;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private List<Child> children;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Bio bio;
public Parent(String name, List children, Bio bio) {
this.name = name;
this.children = children;
this.bio = bio;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child implements Serializable {
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bio implements Serializable {
private String text;
}
Så vil du for eksempel kunne bruge et simpelt JpaRepository
at arbejde med dine objekter:
public interface ParentRepo extends JpaRepository<Parent, Integer> {
}
parentRepo.save(new Parent(
"parent1",
asList(new Child("child1"), new Child("child2")),
new Bio("bio1")
)
);
Parent result = parentRepo.findById(1);
List<Child> children = result.getChildren();
Bio bio = result.getBio();