Hibernate @ManyToMany关系不起作用

时间:2016-10-19 09:29:05

标签: java hibernate

我正在努力与hibernate建立多对多的关系。

我只需要获取每个EcoConfigPrioridadLead对象的产品,但列表是空白的。

电话:

public List<EcoConfigPrioridadLead> getAllPrioridadesLead() {

    List<EcoConfigPrioridadLead> results = em.createNamedQuery("EcoConfigPrioridadLead.getAll").getResultList();

    return results;
}

EcoConfigPrioridadLeads对象的atributte List产品为空白[]。

@Entity EcoConfigPrioridadLead:

@Entity
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@Table(name = "ECO_CONFIG_PRIORIDAD_LEAD", schema="ECOMMERCE")
@NamedQueries({
    @NamedQuery(name = "EcoConfigPrioridadLead.getAll", 
        query = "FROM EcoConfigPrioridadLead p WHERE p.deleted = false"),

    @NamedQuery(name = "EcoConfigPrioridadLead.getOne", query = "FROM EcoConfigPrioridadLead AS p where p.configIdPk = :configIdPk and p.deleted = 0"),
    @NamedQuery(name = "EcoConfigPrioridadLead.delete", query = "UPDATE EcoConfigPrioridadLead p set p.deleted = 1 where p.configIdPk = :configIdPk and p.deleted = 0"),
})

public class EcoConfigPrioridadLead implements Serializable {

    private static final long serialVersionUID = 5579999901294385893L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "ID_PK")
    private Long configIdPk;

    @ManyToOne
    @JoinColumn(name="CONTEXT_ID_FK", nullable = false)
    private EcoTMLeadContext ecoLeadContext;

    @Column(name = "DESCRIPTION", nullable = true)
    private String descripcion;

    @ManyToMany(targetEntity=EcoProduct.class, cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(schema="ECOMMERCE", name="ECO_PRIORIDAD_PRODUCTOS",
        joinColumns=@JoinColumn(name="PIORIDAD_ID_PK", referencedColumnName = "ID_PK"),
        inverseJoinColumns=@JoinColumn(name="PRODUCTO_EEP_PRODUCTCODE", referencedColumnName = "EEP_PRODUCTCODE"))
    private List<EcoProduct> productos;

    @Column(name="COD_PRODUCTO")
    private String codigosProductos;

    @Column(name = "HAS_PRICES", nullable = true)
    private String hasPrices;

    @Column(name="LAST_ENTE_PRIORIDAD")
    private String codigosLastEntePrioridad;

    @Column(name = "PRIORIDAD_INTERNA_VALUE", nullable = false)
    private Long prioridadInternaValue;

    @Column(name = "PRIORIDAD_LEAD_VALUE", nullable = false)
    private Long prioridadLeadValue;

    @Column(name = "DELETED", nullable = false)
    private boolean deleted;

    //getters and setters
}

@Entity EcoProduct:

@Entity
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@Table(name = "EN_ECO_PRODUCTS", schema="RPS")
@NamedQueries({
    @NamedQuery(name = "EcoProduct.getAll", query = "FROM EcoProduct ecoProd"),
})
public class EcoProduct implements Serializable {

    private static final long serialVersionUID = -8629423853963737817L;

    @Column(name = "EEP_ROCODE", nullable = false)
    private Long roCode;

    @Id
    @Column(name = "EEP_PRODUCTCODE")
    private Long productCode;

    @ManyToMany(
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            mappedBy = "productos",
            targetEntity = EcoConfigPrioridadLead.class
        )
    private List<EcoConfigPrioridadLead> prioridad;

    @Column(name = "EEP_ROLABEL", nullable = false)
    private String roLabel;

    @Column(name = "EEP_ENABLED")
    private String enabled;

    @Column(name = "EEP_PRODUCTLINE", nullable = false)
    private Long productLine;

    @Column(name = "EEP_PRODUCTDESC", nullable = false)
    private String productDesc;

    @Column(name = "EEP_MAILINTERN", nullable = false)
    private String mailInterno;

    @Column(name = "EEP_COMPANYMAIL", nullable = true)
    private String companyMail;

    @Column(name = "EEP_COMPANYNAME", nullable = true)
    private String companyName;

    @Column(name = "EEP_COMPANYSNAKEMAIL", nullable = true)
    private String companySnakeMail;

    @Column(name = "EPP_FULLEMISIONRPS", nullable = true)
    private String fullEmisionRPS;

    //getters and setters
}

表:

ECOMMERCE.ECO_PRIORIDAD_PRODUCTOS

| PIORIDAD_ID_PK | PIORIDAD_ID_PK |
|----------------|----------------|
| 2              | 901            |
| 2              | 1500           |
| 3              | 901            |
| 3              | 901            |
| 3              | 1600           |
| 1              | 1600           |

RPS.EN_ECO_PRODUCTS

| EEP_ROCODE | EEP_PRODUCTCODE | EEP_ROLABEL | EEP_ENABLED | EEP_PRODUCTLINE | EEP_PRODUCTDESC | EEP_MAILINTERN   | EEP_COMPANYMAIL  | EEP_COMPANYNAME | EEP_COMPANYSNAKEMAIL | EPP_FULLEMISIONRPS |
|------------|-----------------|-------------|-------------|-----------------|-----------------|------------------|------------------|-----------------|----------------------|--------------------|
| 139        | 7859            | ro139       | 1           | 6969            | blabla          | example@mail.com | example@mail.com | company         | bla bla              |                    |
| 139        | 7856            | ro139       | 1           | 6969            | blabla          | example@mail.com | example@mail.com | company         | blabla               |                    |
| 2000       | 2273            | roLife      | 1           | 2273            | blabla          | example@mail.com | example@mail.com | company         | blablabla            |                    |
| 1600       | 8084            | roMotorapid | 1           | 3000            | bla bla         | example@mail.com | example@mail.com | company         | blablabla            |                    |

ECOMMERCE.ECO_CONFIG_PRIORIDAD_LEAD

| ID_PK | CONTEXT_ID_FK | DESCRIPTION | COD_PRODUCTO        | HAS_PRICES | LAST_ENTE_PRIORIDAD | PRIORIDAD_INTERNA_VALUE | PRIORIDAD_LEAD_VALUE | DELETED |
|-------|---------------|-------------|---------------------|------------|---------------------|-------------------------|----------------------|---------|
| 1     | 1             | bla bla bla | 2273,8933,1004      | S          | 8                   | 8                       | 8                    | 0       |
| 2     | 2             | bla bla bla | 7856,2273,8084,8920 | 9,9        | 9                   | 9                       | 0                    |         |
| 3     | 2             | bla bla bla | 901,1600            | N          | 8                   | 4                       | 2                    | 0       |

Hibernate版本:

  

[org.hibernate.annotations.common.Version](ServerService线程池    - 48)HCANN000001:Hibernate Commons Annotations { 4.0.1.Final-redhat-2 } [org.hibernate.Version](ServerService Thread   游泳池 - 48)HHH000412:Hibernate Core { 4.2.14.SP1-redhat-1 }

0 个答案:

没有答案