سؤال

Why if i run this code:

Student st = new Student();
st.setFirstName("First");
st.setLastName("Last");
st.setIndexNr("11");
st.setStudentPK(new StudentPK(0, user.getIdUser()));
studentFacade.create(st);

Mail m = new Mail();
m.setContent("con");
m.setRecipient("rec");
m.setIdMail(0);
mailFacade.create(m);

List<Mail> l = new ArrayList<Mail>();
l.add(m);
st.setMailList(l);
studentFacade.edit(st);     // st have mailList property set to l

stud=studentFacade.findByIndex("11"); //after edit and get student he has mailList equal null

Why after persist and edit object i get null at property for OneToMany relationship?


In database MySql i have STUDENT table and MAIL table:

CREATE  TABLE IF NOT EXISTS `STUDENT` (
  `id_student` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `first_name` VARCHAR(65) NULL ,
  `last_name` VARCHAR(65) NULL ,
  `index_nr` VARCHAR(45) NULL 
)
ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `MAIL` (
 `id_mail` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
 `recipient` TEXT NULL ,
 `content` TEXT NULL ,
 `sender_student` MEDIUMINT UNSIGNED NULL ,
  PRIMARY KEY (`id_mail`) ,
  INDEX `fk_STUDENT_id_idx` (`sender_student` ASC) ,
  CONSTRAINT `fk_STUDENT`
 FOREIGN KEY (`sender_student` )
 REFERENCES `jkitaj`.`STUDENT` (`id_student` )
 ON DELETE CASCADE
 ON UPDATE CASCADE
)
ENGINE = InnoDB

From database i generate entity in netbeans:

@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {
 private static final long serialVersionUID = 1L;
 @EmbeddedId
 protected StudentPK studentPK;
 @Size(max = 65)
 @Column(name = "first_name")
 private String firstName;
 @Size(max = 65)
 @Column(name = "last_name")
 private String lastName;
 @Size(max = 45)
 @Column(name = "index_nr")
 private String indexNr;
 @OneToMany(mappedBy = "senderStudent",fetch=FetchType.EAGER)
 private List<Mail> mailList;

 //getters, setters ...

}

public class Mail implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue
 @Basic(optional = false)
 @Column(name = " id_mail")
 private Integer idMail;
 @Lob
 @Size(max = 65535)
 @Column(name = "recipient")
 private String recipient; 
 @Lob
 @Size(max = 65535)
 @Column(name = "content")
 private String content;
 @JoinColumn(name = "sender_student", referencedColumnName = "id_student")
 @ManyToOne
 private Student senderStudent;

 //getters, setters...
}

EDIT:

I think i forget about fetch in @OneToMany annotation of Student entity. But when i set in to fetch=FetchType.LAZY i again get null after edit and and get edited object from database. When set fetch=FetchType.EAGER mailList field isn't null. Why ?

هل كانت مفيدة؟

المحلول

Problem is at OpenJPA, when i use fetch=FetchType.LAZY at some property of entity. The problem was with life cycle of enties. All enties must be re-attached to the persistence context.

Same problem is here: OpenJPA - lazy fetching does not work and here: What's the lazy strategy and how does it work?

نصائح أخرى

You have a unique combination of too much information with too little.

If you are actually running that code, then package it up as a complete, runnable example -- yes, it won't run on another machine unless that machine also has a database setup, but it will tell us everything you're doing instead of just the part that you think is important.

Give us the complete error message, don't tell us what it is.

Two important things to remember about posting a question on a site like this: you don't know what's wrong, so you need to give us complete raw data, not half-analysis. And it is generally a waste of time for us to just guess at things.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top