2017-04-13 1 views
2

Je ne parviens pas à récupérer l'image embarquée .J'utilise le démarrage Spring, le repos de données de ressort et le JPA de ressort. J'ai 3 tables dans les données de base
Impossible de récupérer l'objet de ressource incorporé Spring HATEOAS dans le cas d'une relation @ManytoMany et d'une table de recherche avec une colonne supplémentaire

  • utilisateur
  • compétence
  • user_competency (joindre/tableau composite avec colonne supplémentaire)

utilisateur

@Entity 
@Table(name = "\"user\"", schema = "public") 
@JsonIdentityInfo(
      generator = ObjectIdGenerators.IntSequenceGenerator.class, 
      property = "userId") 
public class User implements java.io.Serializable { 

    private Long userId; 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 

    @Column(name = "user_id", unique = true, nullable = false) 
    public Long getUserId() { 
     return this.userId; 
    } 

    public void setUserId(Long userId) { 
     this.userId = userId; 
    } 

    private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0); 

     @OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "user") 
    public Set<UserCompetency> getUserCompetencies() { 
     return this.userCompetencies; 
    } 

    public void setUserCompetencies(Set<UserCompetency> userCompetencies) { 
     this.userCompetencies = userCompetencies; 
    } 

} 

**Competency** 



@Entity 
    @Table(name = "competency", schema = "public") 
    @JsonIdentityInfo(
       generator = ObjectIdGenerators.IntSequenceGenerator.class, 
       property = "competencyId") 
    public class Competency implements java.io.Serializable { 


     private Long competencyId; 
     private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0); 

     @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 

     @Column(name = "competency_id", unique = true, nullable = false) 
     public Long getCompetencyId() { 
      return this.competencyId; 
     } 

     public void setCompetencyId(Long competencyId) { 
      this.competencyId = competencyId; 
     } 

      @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency") 
     public Set<UserCompetency> getUserCompetencies() { 
      return this.userCompetencies; 
     } 

     public void setUserCompetencies(Set<UserCompetency> userCompetencies) { 
      this.userCompetencies = userCompetencies; 
     } 
    } 

UserCompetency

@Entity 
    @Table(name = "user_competency", schema = "public") 
    @JsonIdentityInfo(
       generator =ObjectIdGenerators.IntSequenceGenerator.class, 
       property = "id") 
    public class UserCompetency implements java.io.Serializable { 
     private UserCompetencyId id; 
     private Level level; 
     private User user; 
     private Competency competency; 

     @EmbeddedId 

     @AttributeOverrides({ 
       @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)), 
       @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) }) 
     public UserCompetencyId getId() { 
      return this.id; 
     } 

     public void setId(UserCompetencyId id) { 
      this.id = id; 
     } 

     @ManyToOne(fetch = FetchType.EAGER) 
     @JoinColumn(name = "level_id") 
     public Level getLevel() { 
      return this.level; 
     } 

     public void setLevel(Level level) { 
      this.level = level; 
     } 

     @ManyToOne(fetch = FetchType.EAGER) 
     @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false) 
     public User getUser() { 
return this.user; 
    } 

    public void setUser(User user) { 
     this.user = user; 
    } 

    @ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL) 
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false) 
    public Competency getCompetency() { 
     return this.competency; 
    } 

    public void setCompetency(Competency competency) { 
     this.competency = competency; 
    } 
} 

UserCompetencyId

@Embeddable 
public class UserCompetencyId implements java.io.Serializable { 

    private Long competencyId; 
    private Long userId; 

    public UserCompetencyId() { 
    } 

    public UserCompetencyId(Long competencyId, Long userId) { 
     this.competencyId = competencyId; 
     this.userId = userId; 
    } 


    @Column(name = "competency_id", nullable = false) 
    public Long getCompetencyId() { 
     return this.competencyId; 
    } 

    public void setCompetencyId(Long competencyId) { 
     this.competencyId = competencyId; 
    } 

    @Column(name = "user_id", nullable = false) 
    public Long getUserId() { 
     return this.userId; 
    } 

    public void setUserId(Long userId) { 
     this.userId = userId; 
    } 

    public boolean equals(Object other) { 
     if ((this == other)) 
      return true; 
     if ((other == null)) 
      return false; 
     if (!(other instanceof UserCompetencyId)) 
      return false; 
     UserCompetencyId castOther = (UserCompetencyId) other; 

     return (this.getCompetencyId() == castOther.getCompetencyId()) && (this.getUserId() == castOther.getUserId()); 
    }  
} 

UserCompetencyRepository

public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> { 

} 

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.example</groupId> 
    <artifactId>Demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>Demo</name> 
    <description>Demo api </description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.2.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-rest-hal-browser</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-hateoas</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.restdocs</groupId> 
      <artifactId>spring-restdocs-mockmvc</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-rest-webmvc</artifactId> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

et je veux effectuer GET en utilisant l'URI, il me renvoie un objet incorporé et ne peut pas obtenir la valeur réelle des objets attributs
GET http://localhost:8080/userCompetencies

enter image description here


Comment puis-je obtenir des valeurs d'attribut de l'utilisateur et Objet de compétence où userId = 8 Aide requise

Après la mise en œuvre du problème de projection suggéré non encore résolu et voici la capture d'écran
enter image description here

+0

lié à la question http://stackoverflow.com/questions/43186824/spring-data-rest-how-to-perform-crud-on-manytomany-relation-composite-table -wi/43230963? noredirect = 1 # comment73846465_43230963 – Taimur

+0

s'il vous plaît aussi publier votre fichier pom – KLHauser

+0

@KLHauser J'ai ajouté pom.xml ci-dessus, veuillez revoir it.thanks. – Taimur

Répondre

0

Un moyen serait d'utiliser les projections comme par exemple:

@Projection(name = "edit" , types = Employee.class) 
public interface EditEmployeeProjection { 
    String getFirstName(); 
    String getLastName(); 
    Set<Project> getProjects(); 
} 

Avec cette liste de projets sera intégrée dans le résultat de http://localhost:8080/api/employee/1?projection=edit

Les projections seraient utilisées automatiquement si vous ajoutez excerptProjection à vous référentiel comme décrit ici: How to expose a complete tree structure with Spring Data REST and HATEOAS?

Voir par exemple ici: https://shinesolutions.com/2015/04/15/spring-data-rest-and-projections/

ÉDITÉ

En vous cas une projection ressemblerait à ceci:

@Projection(name = "edit" , types = UserCompetency.class) 
public interface UserCompetencyProjection { 
    User getUser(); 
    Competency getCompetency(); 
} 

Avec http://localhost:8080/userCompetencies?projection=edit alors vous voir le résultat recherché.

enter image description here

Édité 2 le code I utilisé:

Competency.class

@Entity 
@Table(name = "competency", schema = "public") 
@JsonIdentityInfo(
      generator = ObjectIdGenerators.IntSequenceGenerator.class, 
      property = "competencyId") 
public class Competency implements java.io.Serializable { 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "competency_id", unique = true, nullable = false) 
    private Long competencyId; 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency") 
    private List<UserCompetency> userCompetencies = new ArrayList<>(); 

UserCompetency.class

@Entity 
@Table(name = "user_competency", schema = "public") 
@JsonIdentityInfo(
      generator = ObjectIdGenerators.IntSequenceGenerator.class, 
      property = "id") 
public class UserCompetency implements java.io.Serializable { 

    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)), 
     @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) }) 
    private UserCompetencyId id; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false) 
    private User user; 

    @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL) 
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false) 
    private Competency competency; 

UserCompetencyId.clas s

@Embeddable 
public class UserCompetencyId implements java.io.Serializable { 

    @Column(name = "competency_id", nullable = false) 
    private Long competencyId; 

    @Column(name = "user_id", nullable = false) 
    private Long userId; 

UserCompetencyRepository.class

@RepositoryRestResource(excerptProjection = UserCompetencyProjection.class)  
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> { 

After Implementing This ,In my case its not working to show desired jason [![enter image description here][2]][2] 
+0

Je suis incapable d'atteindre la même chose, veuillez suggérer des changements nécessaires dans mon cas, car j'ai 3 objets l'échantillon expliquer sur 2 classes d'objet.Pour réfrence j'ai éditer la question et ajouté la capture d'écran de GET request.thanks – Taimur

+0

Pas de soucis, j'ai ajouté un échantillon .. – KLHauser

+0

même problème persiste, je l'ai essayé essayer.Il montre toujours la liste des objets et ne peut pas récupérer les valeurs des attributs. – Taimur

0

Ça a, en fait, je manquais d'annotation de
@RepositoryRestResource(excerptProjection = UserCompetencyProjection.class) sur la classe UserCompetencyRepository maintenant le look de sortie comme ça je saute comme il est sorti, et mettre nécessaire sortie. enter image description here

enter image description here enter image description here