Ajuda Persistencia com Hibernate

1 resposta
felipe.sodre

Bom dia a todos, não sei persisitir dados em cascata com hibernate, quando persisto o objeto pai o filho (One to Many) não é persistido no banco, alguem poderia me ajudar?

Objeto Pai

package com.sigga.common.map;
/* 
 * @see  General
 * @version 1.0
 * @author Anderson dos Santos
 * @data 05/05/2010
*/

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * Siggasm2Hgeneral generated by hbm2java
 */
@Entity
@Table(name = "SIGGASM2_HGENERAL")
public class General implements java.io.Serializable {

	
	private static final long serialVersionUID = 1L;
	
	private String statusConcl;
	private String statusAprv;
	private String statusAbastece;
	private String statusServico;
	private boolean bloqueiaSincronismo;
	private String orgid;
	private String siteid;
	private Collection<StatusDownload> statusDownloads;
	private int hgeneralId;
	private String statusDownload;
	

	public General() {
	}

	@Id
	@Column(name = "HGENERAL_ID",nullable = false)
	@SequenceGenerator(name="seq_General", sequenceName="SIGGASM2_HGENE_HGENERAL_ID_SEQ")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_General")	
	public int getHgeneralId() {
		return hgeneralId;
	}

	public void setHgeneralId(int hgeneralId) {
		this.hgeneralId = hgeneralId;
	}

	@Column(name = "STATUS_CONCL", length = 30)
	public String getStatusConcl() {
		return this.statusConcl;
	}

	public void setStatusConcl(String statusConcl) {
		this.statusConcl = statusConcl;
	}

	@Column(name = "STATUS_APRV", length = 30)
	public String getStatusAprv() {
		return this.statusAprv;
	}

	public void setStatusAprv(String statusAprv) {
		this.statusAprv = statusAprv;
	}

	@Column(name = "STATUS_ABASTECE", length = 30)
	public String getStatusAbastece() {
		return this.statusAbastece;
	}

	public void setStatusAbastece(String statusAbastece) {
		this.statusAbastece = statusAbastece;
	}

	@Column(name = "STATUS_SERVICO", length = 30)
	public String getStatusServico() {
		return this.statusServico;
	}

	public void setStatusServico(String statusServico) {
		this.statusServico = statusServico;
	}

	@Column(name = "BLOQUEIA_SINCRONISMO", nullable = false, precision = 1, scale = 0)
	public boolean isBloqueiaSincronismo() {
		return this.bloqueiaSincronismo;
	}

	public void setBloqueiaSincronismo(boolean bloqueiaSincronismo) {
		this.bloqueiaSincronismo = bloqueiaSincronismo;
	}

	@Column(name = "ORGID", nullable = false, length = 8)
	public String getOrgid() {
		return this.orgid;
	}

	public void setOrgid(String orgid) {
		this.orgid = orgid;
	}

	@Column(name = "SITEID", nullable = false, length = 8)
	public String getSiteid() {
		return this.siteid;
	}

	public void setSiteid(String siteid) {
		this.siteid = siteid;
	}
	
	/*** Relacionamento com a entidade StatusDonwload */
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy="general", targetEntity=StatusDownload.class,
			cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@Fetch(FetchMode.SELECT)
	public Collection<StatusDownload> getStatusDownloads() {
		if(this.statusDownloads ==  null)
			statusDownloads = new ArrayList<StatusDownload>();
		return statusDownloads;
	}

	public void setStatusDownloads(Collection<StatusDownload> statusDownloads) {
		this.statusDownloads = statusDownloads;
	}
	
	@Transient
	public String getStatusDownload() {
		return statusDownload;
	}

	public void setStatusDownload(String statusDownload) {
		this.statusDownload = statusDownload;
	}

}

Objeto Filho

package com.sigga.common.map;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "SIGGASM2_HSTATUS_DOWNLOAD")
public class StatusDownload implements Serializable{

	private static final long serialVersionUID = 1L;

	private int hstatusdonwloadId;
	private String status;
	private int hgeneralId;
	private General general;
	
	@Id
	@Column(name="HSTATUS_DOWNLOADID",nullable = false)
	@SequenceGenerator(name="seq_statusDownload", sequenceName="SIGGASM2_HSTA_HSTATUS_DOWN_SEQ")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_statusDownload")
	public int getHstatusdonwloadId() {
		return hstatusdonwloadId;
	}
	public void setHstatusdonwloadId(int hstatusdonwloadId) {
		this.hstatusdonwloadId = hstatusdonwloadId;
	}
	
	@Column(name="STATUS", nullable = false, length = 30)
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	@Column(name = "HGENERAL_ID", unique = true, nullable = false, precision = 126, scale = 0)
	public int getHgeneralId() {
		return hgeneralId;
	}

	public void setHgeneralId(int hgeneralId) {
		this.hgeneralId = hgeneralId;
	}
	
	/*** Relacionamento com a entidade General ***/
	
	@ManyToOne(fetch = FetchType.EAGER, targetEntity=General.class, 
			cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinColumn(name="HGENERAL_ID", insertable=false, updatable=false)
	@Fetch(FetchMode.SELECT)
	public General getGeneral() {
		return general;
	}
	public void setGeneral(General general) {
		this.general = general;
	}
}

Forma que estou persistindo

public void save() {
		
		try {
			StatusDownload statusDownload = new StatusDownload();
			General general = new General();
			
			general.setOrgid(this.getSm2General().getOrgID());
			general.setSiteid(this.getSm2General().getOrgID());
			general.setStatusConcl(this.statusConcl);
			general.setStatusAprv(this.statusAprv);
			general.setStatusAbastece(this.statusEmerg);
			general.setStatusServico(this.statusAprvOS);
			
			for(String status : this.statusImport){
				statusDownload = new StatusDownload();
				statusDownload.setStatus(status);
				general.getStatusDownloads().add(statusDownload);
			}
			
			new GeneralFacade().SalvarGeneral(general);
			new StatusDownloadFacade().save(statusDownload);
			
			
		} catch (Exception e) {
			FacesMessage message = new FacesMessage(e.toString());
			FacesContext.getCurrentInstance().addMessage("Error", message);
		}
	}

Por favor me ajudem!!!

1 Resposta

felipe.sodre

Niguem??? :frowning:

Criado 4 de junho de 2010
Ultima resposta 4 de jun. de 2010
Respostas 1
Participantes 1