Can't persist an entity with two foreign keys pointing to same table
I have three database-tables mapped as three entities:
Employee
Room
EmployeeToRoom
public class Employee implements Serializable {
@Id
@Column(name = "\"id_emp\"")
private Integer id_emp;
@Column(name = "\"name\"")
private String name;
@Column(name = "\"surname\"")
private String surname;
...
}
public class Room implements Serializable {
@Id
@Column(name = "\"id_room\"")
private Integer id_room;
@ManyToOne
@JoinColumn(name = "\"id_roomtype\"")
private RoomType type;
@ManyToOne
@JoinColumn(name = "\"id_floor\"")
private Floor floor;
...
}
public class EmployeeToRoom implements Serializable {
@Id
@SequenceGenerator(name = "etr_seq", sequenceName = "etr_seq",
allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "etr_seq")
@Column(name = "\"id\"")
private Integer id;
@ManyToOne
@JoinColumn(name = "\"id_room\"")
private Room room;
@ManyToOne
@JoinColumn(name = "\"id_emp\"")
private Employee employee;
/* Who created/updated record in DB */
@ManyToOne
@JoinColumn(name = "\"who\"")
private Employee who;
/* When was record created/updated */
@Column(name = "\"when\"")
private Timestamp when;
private static final long serialVersionUID = -2059394677268317393L;
...
}
So I'd like to store info which employee works in which room (and also who
and when created database record).
I created Managed Bean with listener:
@ManagedBean
@ViewScoped
public class EmployeeToRoomBean implements Serializable {
private static final long serialVersionUID = 1L;
final javax.persistence.EntityManager em = EntityManager.getEm();
private Logger log = LoggerFactory.getLogger(EmployeeToRoomBean.class);
private EmployeeToRoomDaoJPA ETRdao = new EmployeeToRoomDaoJPA();
...
public void updateListener() {
TimeStampMaker tsm = new TimeStampMaker();
...
/* Some issues */
...
EmployeeToRoom etrToadd = new EmployeeToRoom();
etrToadd.setRoom(getEditedRoom());
etrToadd.setEmployee(employee);
etrToadd.setWho(UserLogin.getUser().getEmployee());
etrToadd.setWhen(tsm.getTimestampAsObject());
ETRdao.create(etrToadd);
}
}
If I try to make logger output with set values (setRoom(), setEmployee(),
setWho(), setWhen()) everything is OK - not null values.
And finnaly I have:
public class EmployeeToRoomDaoJPA implements EmployeeToRoomDao,
Serializable {
private static final long serialVersionUID = 1L;
private Logger log =
LoggerFactory.getLogger(EmployeeToRoomDaoJPA.class);
...
@Override
public void create(EmployeeToRoom entity) {
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
this.log.info("Creating new EmployeeToRoom entity:");
this.log.info("Room: " + entity.getRoom().getId_room());
this.log.info("Employee: " +
entity.getEmployee().getId_emp());
this.log.info("Who: " + entity.getWho().getId_emp());
this.log.info("When: " + entity.getWhen().toString());
em.persist(entity);
tx.commit();
this.log.info("done inserting obj " + entity.getId());
} catch (PersistenceException pe) {
if (tx != null) {
tx.begin();
tx.rollback();
}
throw pe;
}
}
}
And there's my problem. I just receive an PersistenceException with
message that query violated not-null constraint of column Who in
EmployeeToRoom entity.
But... I added logger output immediatly after beginning a transaction.
There's not null value in entity.getWho().getId_emp().
I can't solve this problem for several days. As you can see I have two
foreign keys in EmployeeToRoom entity/table to Employee entity/table -
property Employee and property Who. Can it be my problem? Or everything
else is wrong?
Many thanks for any advices.
(Postgres DBMS, Apache Tomcat, Hibernate, JSF2/Mojarra)
No comments:
Post a Comment