Reputation: 456
I have tried every other option in the net. But could not successfully insert records in both Tickets and Messages table (foreign key mapped) simultaneously.
Can you identify what is wrong? When I run the code, I am getting records only in the tickets table and not in the messages table.
Below is the Ticket.class
@Entity
@Table(name = "ticket")
public class Ticket {
@Id
@Column(name = "ticket_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int ticket_id;
@Column(name = "ticket_desc")
String ticket_desc;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
Set<Message> message = new HashSet<Message>();
public int getTicket_id() {
return ticket_id;
}
public void setTicket_id(int ticket_id) {
this.ticket_id = ticket_id;
}
public String getTicket_desc() {
return ticket_desc;
}
public void setTicket_desc(String ticket_desc) {
this.ticket_desc = ticket_desc;
}
public Set<Message> getMessage() {
return message;
}
public void setMessage(Set<Message> message) {
this.message = message;
}
}
Below is the Message.class
@Entity
@Table(name = "message")
public class Message {
@Id
@Column(name = "message_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int message_id;
@Column(name = "ticket_id")
private int ticket_id;
@Column(name = "message_text")
private String message_text;
@JoinColumn(name = "ticket_id", insertable = false, updatable = false)
@ManyToOne(cascade=CascadeType.ALL)
private Ticket ticket;
public Ticket getTicket() {
return ticket;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
public int getMessage_id() {
return message_id;
}
public void setMessage_id(int message_id) {
this.message_id = message_id;
}
public int getTicket_id() {
return ticket_id;
}
public void setTicket_id(int ticket_id) {
this.ticket_id = ticket_id;
}
public String getMessage_text() {
return message_text;
}
public void setMessage_text(String message_text) {
this.message_text = message_text;
}
}
Below is the test()
public static void test() {
Session session = HibernateSession.getHibernateSession();
Transaction t = session.beginTransaction();
Ticket ticket = new Ticket();
ticket.setTicket_desc("ticket description 2");
Set<Message> messages = new HashSet<Message>();
Message message = new Message();
message.setMessage_text("This is message text 2");
message.setTicket(ticket);
messages.add(message);
ticket.setMessage(messages);
session.persist(ticket);
t.commit();
}
Can you identify what is wrong? When I run the code, I am getting records only in the tickets table and not in the messages table.
Upvotes: 0
Views: 894
Reputation: 21153
The reason is because your mapping does not cascade the persist operation, see:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
Set<Message> message = new HashSet<Message>();
Should you want the cascade operation to occur, you need to specify that
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ticket")
Set<Message> message = new HashSet<Message>();
Cascade operations are extremely useful, but be mindful of their potential side effects, particularly if you modify Ticket
which you don't intend for an operation to be cascaded to Message
.
If you want to manage the persistence state of Message
and Ticket
separately, you'd need to modify your business logic code to persist the Message
s and then persist the Ticket
.
Upvotes: 2