[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6844) One-To-One fails to delete both sides if constraint violation occurs

Jana (JIRA) noreply at atlassian.com
Wed Nov 30 19:47:19 EST 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6844?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44482#comment-44482 ] 

Jana commented on HHH-6844:
---------------------------

Yes! That fixed it! Thank you.

> One-To-One fails to delete both sides if constraint violation occurs
> --------------------------------------------------------------------
>
>                 Key: HHH-6844
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6844
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.6.8
>         Environment: Using hibernate version 3.6.8 on a mac OS 10.x.
>            Reporter: Jana
>         Attachments: testHibernate.zip
>
>
> In this example there are two object: User and Credentials and they have a unidirectional one-to-one relationship. If User fails to persist due to a constraint violation on the emailAddress field, then the Credentials are saved but not removed when the exception is thrown. So now in the database there is no User object but there is a Credentials object. I have set the cascade type to ALL so my expectations would be if an error occurs during persisting the User that neither object remains in the database. As a workaround I go and check for orphans. I've tried a bi-directional one-to-one to see if that made any difference but it also failed to cleanup after the exception.
> Below is the code. I left out the setters/getters to condense the code. I know the orphanRemoval=true is redundant since I have cascadeType=ALL but was trying different ways to force the orphan to be removed so left it there in this example.
> To test:
> public class Test {
>    public void testPersist() {
>       User user = new User();
>       Credentials credentials = new Credentials();
>       credentials.setUsername("username");
>       credentials.setPassword("password");
>       user.setCredentials(credentials);
>       user.setEmailAddress("myemail at wherever.com");
>       user.persist(); // this will persist as expected.
>       User user2 = new User();
>       Credentials credentials2 = new Credentials();
>       credentials2.setUsername("username2");
>       credentials2.setPassword("password");
>       user2.setCredentials(credentials2);
>       user2.setEmailAddress("myemail at wherever.com"); // same email address so will cause a constraint violation
>       user2.persist(); // this will throw a constraint violation. User2 won't be persisted but credentials will leaving an orphan
>    }
> }
> public class User {
>     @Id
>     @GeneratedValue(strategy = GenerationType.AUTO)
>     @Column(name = "id")
>     private Long id;
>     @NotNull
>     @Column(unique = true)
>     @Size(min = 1)
>     private String emailAddress;
>     @OneToOne(targetEntity=Credentials.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
>     private Credentials credentials;
> }
> public class Credentials {
>     @NotNull
>     @Column(unique = true)
>     @Size(min = 1, max=255)
>     private String username;
>     
>     @NotNull
>     @Size(min = 1, max=255)
>     private String password;
> }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list