Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate saves empty Spring autobound ojects
    primarykey
    data
    text
    <p>I'm using Spring + JSF2 + Hibernate to build a web application. Two relevant classes look like this:</p> <pre><code>@Entity @Table(name="people") @Named public class Person implements Serializable { private int id; private String forename; @Inject private PhoneNumber mobile_phone; /* Other properties */ @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="mobile_phone_number_id", nullable = true) public PhoneNumber getMobilePhone() { return mobile_phone; } /* Other getters and setters */ } @Entity @Table(name="phone_numbers") @Named public class PhoneNumber { private Integer id; private String code; private String number; /* Other stuff */ } </code></pre> <p>Dependency injection is handled by Spring (<code>@Named</code>, <code>@Inject</code>). Database persistence is configured through Hibernate annotations (<code>@OneToOne</code>, <code>@JoinColumn</code>). JSF form elements are bound to these classes using EL.</p> <p><strong>My question is</strong> how to tell hibernate no to save autobound PhoneNumber if all its fields are left blank (the user doesn't submit any values)? In other words, I want the Person object to be saved but don't want records with empty strings appear in the <code>phone_numbers</code> table.</p> <p>And two bonus questions if someone knows.</p> <ol> <li>When does Spring bind the PhoneNumber - at Person creation or when a request for a PhoneNumber comes from JSF?</li> <li>[SOLVED] I have fields in my form which aren't marked with the <code>required</code> attribute in JSF. Obviously, if such fields are left empty, validation shouldn't be triggered. The problem is that when I configured Hibernate validator, such non-required fields are now validated even when left blank. Is there any way to skip validation for empty fields which aren't marked as <code>required</code> in JSF? I tried to set <code>&lt;prop key="javax.persistence.validation.mode"&gt;CALLBACK&lt;/prop&gt;</code> in <code>hibernateProperties</code> of <code>SessionFactory</code> bean, but this didn't help.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload