如何使用ManyToMany关系填充Spring Data Repositories

时间:2016-12-14 21:08:35

标签: spring spring-mvc spring-data spring-data-jpa

我想使用用户角色和两个与这些角色相关的初始用户填充存储库。

这是我要上传的JSON:

[ 
    {
        "_class": "models.Role",
        "@id": 1,
        "name": "ROLE_BLOG_ADMIN",
        "description": "Rol de los administradores del blog"
    },
    {
        "_class": "models.Role",
        "@id": 2,
        "name": "ROLE_BLOG_CONTRIBUTOR",
        "description": "Rol de los editores de artículos"
    },
    { 
        "_class": "models.User",
        "username": "sergio11",
        "password": "$2a$10$0eCQpFRdw8i6jJzjj/IuNuKpJYnLaO5Yp9xSJ3itcfPmQNXVhmNyu",
        "email": "gfhdsgfjhdsgfjhdsgf@gmail.com",
        "fullName": "Sergio Sánchez Sánchez",
        "roles": [1, 2]
    },
    {
        "_class": "models.User",
        "username": "dani33",
        "password": "$2a$10$0eCQpFRdw8i6jJzjj/IuNuKpJYnLaO5Yp9xSJ3itcfPmQNXVhmNyu",
        "email": "danihiglesias@usal.es",
        "fullName": "Daniel de la Iglesia",
        "roles": [2]
    }
]

我在角色实体中使用JsonIdentityInfo

@Entity
@Table(name = "roles")
@JsonIdentityInfo(generator=IntSequenceGenerator.class, property="@id")
public class Role implements Serializable

我在上下文中包含了Jackson2RepositoryPopulatorFactoryBean

@Bean(name="repositoryPopulator")
    public Jackson2RepositoryPopulatorFactoryBean  provideJackson2RepositoryPopulatorFactoryBean(){
        Resource sourceData = new ClassPathResource("data.json");
        Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
        factory.setResources(new Resource[] { sourceData });
        return factory;
    }

但是,没有角色与任何用户相关联。

这是协会:

@ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
      name="USER_ROLES",
      joinColumns=@JoinColumn(name="USER_ID", referencedColumnName="ID"),
      inverseJoinColumns=@JoinColumn(name="ROLE_ID", referencedColumnName="ID"))
    private Set<Role> roles;

有谁知道如何修复它们?

1 个答案:

答案 0 :(得分:1)

它应该只是抱怨缺少Role实体的构造函数,对我而言,一切顺利,以下内容:

@Entity
@Table(name = "roles")
public class Role implements Serializable {

    public Role() {
    }

    public Role(Integer id) {
        this.id = id;
    }

    @Id
    @JsonProperty("@id")
    private Integer id;

   //additional properties, getters & setters
   ...  
}

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name="USER_ROLES",
            joinColumns=@JoinColumn(name="USER_ID", referencedColumnName="ID"),
            inverseJoinColumns=@JoinColumn(name="ROLE_ID", referencedColumnName="ID"))
    private Set<Role> roles;

    //additional properties, getters & setters
    ...
}

我得到了:

[  
   {  
      "id":1,
      "roles":[  
         {  
            "name":"ROLE_BLOG_ADMIN",
            "@id":1
         },
         {  
            "name":"ROLE_BLOG_CONTRIBUTOR",
            "@id":2
         }
      ],
      "email":"sss4esob@gmail.com"
   },
   {  
      "id":2,
      "roles":[  
         {  
            "name":"ROLE_BLOG_CONTRIBUTOR",
            "@id":2
         }
      ],
      "email":"danihiglesias@usal.es"
   }
]

如果这对您不起作用,您能提供更多实体代码吗?你遇到任何例外吗?