Announcement Announcement Module
Collapse
No announcement yet.
Many To Many with attribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Many To Many with attribute

    Hi Guys...

    Iīm using spring data and I canīt persist my new entity with there relationships...
    I also canīt update .. Some error, but one thing at the time..

    I have the following mapping:

    [code=java]

    @Table(name = "com_prospecto")
    @Entity
    public class Prospecto {

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

    @ManyToMany
    @JoinTable(name = "com_historico_acoes", joinColumns = @JoinColumn(name = "id_prospecto"), inverseJoinColumns = @JoinColumn(name = "id_acao"))
    private List<Acao> acoes;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "prospecto")
    private List<ProspectoCampo> prospectoCampos;

    [/code]
    And I have:
    [code=java]

    @Table(name = "com_prospecto_campo")
    @Entity
    @IdClass(ProspectoCampo.class)
    public class ProspectoCampo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @ManyToOne
    @JoinColumn(name = "id_prospecto")
    private Prospecto prospecto;

    @Id
    @ManyToOne
    @JoinColumn(name = "id_campo")
    private Campo campo;

    private String valor;
    [/code]
    And Iīm trying to save like this:
    [code=java]
    public String salvar(){
    Prospecto prospecto = new Prospecto();
    List<ProspectoCampo> prospectoCampos = new ArrayList<ProspectoCampo>();
    List<Acao> prospectoAcoes = new ArrayList<Acao>();

    for(ProspectoHelper hp : ProspectoHelper.extrairDadoDoModelo(dynaFormModelA tual)){
    ProspectoCampo pc = new ProspectoCampo();
    pc.setCampo(campoService.findOne(hp.getPropertyFil ter().getCampo().getId()));
    pc.setValor(hp.getPropertyFilter().getValue().toSt ring());
    pc.setProspecto(prospecto);
    prospectoCampos.add(pc);
    }

    prospecto.setProspectoCampos(prospectoCampos);
    prospecto.setAcoes(prospectoAcoes);

    try{
    prospectoService.save(prospecto);
    }catch(Exception e){
    log.error("Erro ao salvar Prospecto!", e);
    messageUtil.sendErrorMessageToUser("global.error", "global.insert.error", e.getMessage());
    return null;
    }

    return getRadarPath();
    }
    [/code]
    Iīm using Spring Data!

    What happends Spring Data persist the "Prospecto" entity, but doesnīt persist its relationship (ProspectoCampos and Acoes).

    Am I doing something wrong?
    thanks!
Working...
X