Sping Data返回null元素列表,但是List包含很多元素

时间:2019-09-05 16:01:48

标签: java spring-boot jpa spring-data-jpa spring-data

我有问题

我有两个实体:

实体ALBERO

@Entity 
@IdClass(Albero.class)
@Table(schema="organo", name = "albero")
public class Albero implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @JoinColumn(name = "cmu")
    @OneToOne
private Struttura cmu;

    @Id
    @JoinColumn(name = "padre")
    @NotFound(action = NotFoundAction.IGNORE)
    @ManyToOne 
    private Struttura padre;

    @Column(name = "div")
    private Date div;

@Column(name = "dfv")
private Date dfv;

    @Column(name = "cso", length=15)
    private String cso;

... get and set methods

和实体STRUTTURA

@Entity
@Table(schema="organo", name="strutture")
@SqlResultSetMapping(
        name = "Albero",
        classes =  @ConstructorResult(
                    targetClass = Albero.class,
                    columns = {
                @ColumnResult(name="cmu", type=String.class), 
                    @ColumnResult(name="padre", type=String.class)
                    }
        )       
)
public class Struttura implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "cmu")
    private String cmu; 

    @Column(name = "nome", length=512)
    private String nome;

    @Column(name = "tipologia")
    private String tipologia;

    @Column(name = "data_creazione")
    private Date data_creazione;

 ...get and set methods 

我有一个方法为

的存储库AlberoRepository
public List<Albero> findByDfvIsNull();

和其他带有本机查询的方法:

String QUERY = "SELECT a.* FROM ALBERO a WHERE DFV IS NULL";
@Query(nativeQuery = true, value = QUERY)
public List<Albero> findAllWithDfvIsNull();
在Oracle DB上执行

La查询,给我一个802记录的结果。 每个记录已满;它们具有值

相反,方法Java,给我一个802对象列表 但这些对象为空

为什么? 你能帮我吗?

非常感谢您

1 个答案:

答案 0 :(得分:0)

@IdClass批注中的问题。 您需要PK的特殊课程:

public class AlberPK implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @JoinColumn(name = "cmu")
  @OneToOne
  private Struttura cmu;

  @Id
  @JoinColumn(name = "padre", nullable = true)
  @NotFound(action = NotFoundAction.IGNORE)
  @ManyToOne
  private Struttura padre;
}

...,然后在Albero.class中稍作更改:

@Entity
@IdClass(AlberPK.class)
@Table(schema = "organo", name = "albero")
public class Albero implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  private Struttura cmu;
  @Id
  private Struttura padre;

  ...
}

必须工作。

我真的不明白-为什么您使用复杂的PK?如果可能,请重新考虑您的数据库结构。将来会对您有很大帮助。