Domanda

Sono semplice intitolmente di classe con i @EmbeddedId (Integer e String campi in classe separata).E io uso i dati a molla (org.springframework.data.jpa.repository.JpaRepository) per accedere al database (MySQL), con l'ID normale Le query funzionano bene, sia il generato da primavera che quelli scritti da solo.Con EmbeddedId non sono riuscito a creare la query corretta.Quello che voglio fare è selezionare tutto l'ID (uno dei campi di EmbedDedID per il quale si verifica una condizione) qui hai alcuni campioni di codice, forse qualcuno avrà un'idea come risolverlo.
La classe di entità:

@Entity
@Table(name="table_name")
public class EntityClass {

    @EmbeddedId
    private EmbeddedIdClass id;
    private String  someField;
    //rest of implemetation
}
.

La classe EmbedDedID:

@Embeddable
public class EmbeddedIdClass implements Serializable {

public EmbeddedIdClass(Long id, String language) {
    super();
    this.id = id;
    this.language = language;
}

public UserAdTextId() {}        

@Column(name="ad_id", nullable=false)
    private Integer id;

    @Column(name="language_code", nullable=false)
    private String  language;
    //rest of implemetation
}
.

E il repository:

@Transactional(readOnly=true)
public interface MyRepository extends JpaRepository<EntityClass, EmbeddedIdClass> {
    @Query("select distinct ad_id from EntityClass where userId = :userId and (/*here the conditions*/)")
    public Page<Integer> findUserAdsWithSearchString(@Param("userId") Integer userId, @Param("searchString") String searchString, Pageable page);
//rest of implemetation
}
.

Non ho trovato alcuna documentazione Come creare i metodi per supportare il @EmbedDedID, stavo provando molti nomi di metodi diversi, ma ottengo sempre eccezioni dal metodo parser ..

È stato utile?

Soluzione

It seems your query is using column names. It should contain the property names, including navigation into embedded objects. There's also a related question here on SO: How to write JPQL SELECT with embedded id?

select distinct id.id from EntityClass where userId = :userId and (...)

The first id refers to attribute id of EntityClass (of type EmbeddedIdClass), and the second one refers to the id property of EmbeddedIdClass.

Also, make sure there's a userId property in EntityClass.

Altri suggerimenti

(by Yosi Lev) This can be done as the following: Suppose your main entity is:

@Entity
@Table(name="JRULES_FLOW")
public class JrulesFlow implements Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private JrulesFlowPK id;

   @Column(name="NEXT_SEQ")
   private int nextSeq;

   @Column(name="REF_ID")
   private String refId;

   @Column(name="TASK_TYPE")
   private String taskType;

   @Column(name="VALUE_TO_FIND")
   private String valueToFind;
}

And your PK class is :

@Embeddable
public class JrulesFlowPK implements Serializable {
   //default serial version id, required for serializable classes.
   private static final long serialVersionUID = 1L;

   @Column(name="FLOW_ID")
   private String flowId;

   @Column(name="TASK_SEQ")
   private long taskSeq;
 }

The JPA repository method name shouls include the name of the id field in the main class followed by the property you want to query uppon within the PK class:

public interface JrulesFlowRepository extends JpaRepository<JrulesFlow, 
      JrulesFlowPK> { // NOTE: put here both classes - also the pk class..
   public List<JrulesFlow>  findByIdFlowId(String flowId);  // Id - is the 
                  // @EmbeddedId in JrulesFlow. FlowId is an attribute 
                  // within JrulesFlowPK
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top