I am not sure what the problem is I ran a test case and everything looks good.
Comment
Code:
@Entity
public class Comment extends AbstractPersistable<Long> {
private static final long serialVersionUID = 1804213477250265140L;
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Customer
Code:
@Entity
public class Customer extends AbstractPersistable<Long> {
private static final long serialVersionUID = 118458269657011416L;
@OneToMany(cascade = CascadeType.ALL)
private Set<Comment> comments = new HashSet<Comment>();
public Set<Comment> getComments() {
return comments;
}
public void setComments(Set<Comment> comments) {
this.comments = comments;
}
}
Repository
Code:
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
Test Case
Code:
@ContextConfiguration(classes = { ApplicationConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("standard")
@Transactional
@TransactionConfiguration(defaultRollback = false)
public class RepositoryTest {
@Autowired
private CustomerRepository customerRepository;
@Before
public void init() {
Customer customer = new Customer();
Comment comment = new Comment();
comment.setText("test");
customer.getComments().add(comment);
customerRepository.saveAndFlush(customer);
System.out.println("Save 1");
}
@Test
public void testSaveEmployee() throws Exception {
Comment comment = new Comment();
comment.setText("test2");
List<Customer> customers = customerRepository.findAll();
// There will only be one customer
Customer customer = customers.get(0);
for (Comment c : customer.getComments()) {
System.out.println(c.getText());
}
customer.getComments().add(comment);
Customer persistedCustomer = customerRepository.saveAndFlush(customer);
System.out.println("save 2");
for (Comment c : persistedCustomer.getComments()) {
System.out.println(c.getText());
}
}
}
Here is the output from the test case (which really was not testing anything :P)
Hibernate: insert into Customer (id) values (default)
Hibernate: values identity_val_local()
Hibernate: insert into Comment (id, text) values (default, ?)
Hibernate: values identity_val_local()
Hibernate: insert into Customer_Comment (Customer_id, comments_id) values (?, ?)
Save 1
Hibernate: select customer0_.id as id1_ from Customer customer0_
test
Hibernate: insert into Comment (id, text) values (default, ?)
Hibernate: values identity_val_local()
Hibernate: insert into Customer_Comment (Customer_id, comments_id) values (?, ?)
save 2
test
test2
I also logged into the database I was using a derby client DB and verified that the data was correct after running.
Not sure if this helps you or not.
Thanks,