Cascade Problem (Hibernate)

Status
Nicht offen für weitere Antworten.

GilbertGrape

Bekanntes Mitglied
Ich versteh das irgendwie immernoch nicht so richtig.

Also, ich hab 2 Tabellen mit einer m-n Beziehung und eine Verknüpungstabelle mit einer eigenen Klasse.
In der hab ich an den 2 Beziehungen, den Cascade-Typ auf PERSIST gesetzt. Darunter verstehe ich, dass er beim persistieren die beiden anderen Objekte auch speichert (falls noch nicht vorhanden?). Das funktioniert aber so bei mir nicht. Ich kann ja mal etwas Code posten:
aus der Verknüpfungstabelle:
Code:
@ManyToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "unmodifiedText",insertable = false,	updatable = false)
	private UnmodifiedText unmodifiedText;
	@ManyToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "paragraph",insertable = false,	updatable = false)
	private Paragraph paragraph;

aus der einen Tabellen (Paragraph):
Code:
@OneToMany(mappedBy = "paragraph")
	public Set<UnmodifiedText_Paragraph> getUtp() {
		return utp;
	}

	public void setUtp(Set<UnmodifiedText_Paragraph> utp) {
		this.utp = utp;
	}

public UnmodifiedText_Paragraph addUnmodifiedText(Integer pos, UnmodifiedText text){
		UnmodifiedText_Paragraph utp = new UnmodifiedText_Paragraph(this, pos, text);
		this.getUtp().add(utp);
		text.getUtp().add(utp);
		return utp;
	}

aus der 2. Tabelle (UnmodifiedTExt):

Code:
@OneToMany(mappedBy = "unmodifiedText")
	public Set<UnmodifiedText_Paragraph> getUtp() {
		return utp;
	}

	public void setUtp(Set<UnmodifiedText_Paragraph> utp) {
		this.utp = utp;
	}
	
	public UnmodifiedText_Paragraph addParagraph(Integer pos, Paragraph paragraph){
		UnmodifiedText_Paragraph utp = new UnmodifiedText_Paragraph(paragraph, pos, this);
		this.getUtp().add(utp);
		paragraph.getUtp().add(utp);
		return utp;
	}

und hier dann der Test:

Code:
DaoFactory factory = DaoFactory.instance(DaoFactory.HIBERNATE);
		//paragraphDao = factory.getParagraphDao();
		//paragraphDao.begin();
		//unmodTextDao = factory.getUnmodifiedTextDao();
		//unmodTextDao.begin();		
		utpd = factory.getUnmodifiedText_ParagraphDao();
		utpd.begin();

               Paragraph p = new Paragraph();
		p.setText("noch meeeeeehr Text. Bald ist Wochenende!");
		UnmodifiedText unmodText = new UnmodifiedText();
		unmodText.setText("aber erst noch Mittag!");
	//	paragraphDao.save(p);
	//	Long id = unmodTextDao.save(unmodText);
		UnmodifiedText_Paragraph utp = unmodText.addParagraph(1, p);
		utpd.save(utp);

          //      if (paragraphDao.getSession().isOpen()) {
	//	 paragraphDao.commit();
	//	}
	//	if (unmodTextDao.getSession().isOpen()) {
	//	 unmodTextDao.commit();
	//	}
		if (utpd.getSession().isOpen()) {
			 utpd.commit();
			}

Ich würde denken, dass es so funktionieren würde, tut es aber nicht. Er sagt dann immer, dass er null in die Verknüpfungstabelle eintragen würde und das nicht geht.
Wenn ich das Auskommentierte einkommentiere funktioniert es. Aber wieso geht das mit dieser Cascade nicht? ODer versteh ich das falsch?

Danke schonmal,
GG
 
Mit solchen unübersichtlichen und unvollständigen Code Snippets machst Du es einem nicht einfach, Dir zu helfen.
 
dann hier nochmal (fast) die kompletten Klassen:

1. Tabelle (die eine Hälfte der m-n-Beziehung)
Code:
@Entity
@Table
public class Paragraph {

	Long id;
	String text;
	Set<UnmodifiedText_Paragraph> utp = new HashSet<UnmodifiedText_Paragraph>();

	public Paragraph() {

	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Lob
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	@OneToMany(mappedBy = "paragraph")
	public Set<UnmodifiedText_Paragraph> getUtp() {
		return utp;
	}

	public void setUtp(Set<UnmodifiedText_Paragraph> utp) {
		this.utp = utp;
	}
	
	public UnmodifiedText_Paragraph addUnmodifiedText(Integer pos, UnmodifiedText text){
		UnmodifiedText_Paragraph utp = new UnmodifiedText_Paragraph(this, pos, text);
		this.getUtp().add(utp);
		text.getUtp().add(utp);
		return utp;
	}

}

hier die andere Seite der Beziehung:
Code:
@Entity
@Table(name = "UNMODIFIED_TEXT")
public class UnmodifiedText {

	Long id;
	String text;
	Set<UnmodifiedText_Paragraph> utp = new HashSet<UnmodifiedText_Paragraph>();
	
	public UnmodifiedText() {

	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	@Lob
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	@OneToMany(mappedBy = "unmodifiedText")//,cascade=CascadeType.PERSIST)
	public Set<UnmodifiedText_Paragraph> getUtp() {
		return utp;
	}

	public void setUtp(Set<UnmodifiedText_Paragraph> utp) {
		this.utp = utp;
	}
	
	public UnmodifiedText_Paragraph addParagraph(Integer pos, Paragraph paragraph){
		UnmodifiedText_Paragraph utp = new UnmodifiedText_Paragraph(paragraph, pos, this);
		this.getUtp().add(utp);
		paragraph.getUtp().add(utp);
		return utp;
	}

}

Die Verknüpfungstabelle:

Code:
@Entity
@Table
public class UnmodifiedText_Paragraph {
	
	@Embeddable
	public static class Id implements Serializable{
		private Long unmodifiedText;
		private Long paragraph;
		
		public Id(){
			super();
		}
		
		public Id(Long unmodifiedText, Long paragraph){
			super();
			this.unmodifiedText = unmodifiedText;
			this.paragraph = paragraph;
		}

		
	}
	
	@EmbeddedId
	private Id id = new Id();
	@Column
	private Integer position;
	@ManyToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "unmodifiedText",insertable = false,	updatable = false)
	private UnmodifiedText unmodifiedText;
	@ManyToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "paragraph",insertable = false,	updatable = false)
	private Paragraph paragraph;
	
	public UnmodifiedText_Paragraph(){
		super();
	}

	public UnmodifiedText_Paragraph(Paragraph paragraph, Integer position,
			UnmodifiedText unmodifiedText) {
		super();
		this.paragraph = paragraph;
		this.position = position;
		this.unmodifiedText = unmodifiedText;
		this.id.paragraph = paragraph.getId();
		this.id.unmodifiedText = unmodifiedText.getId();
		
		paragraph.getUtp().add(this);
		unmodifiedText.getUtp().add(this);
	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public UnmodifiedText getUnmodifiedText() {
		return unmodifiedText;
	}

	public void setUnmodifiedText(UnmodifiedText unmodifiedText) {
		this.unmodifiedText = unmodifiedText;
	}

	
	public Paragraph getParagraph() {
		return paragraph;
	}

	public void setParagraph(Paragraph paragraph) {
		this.paragraph = paragraph;
	}	
	
}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben