Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Default cascading behavior for a ManyToOne relationship ?

$
0
0
Hi,

I have a ManyToOne unidirectional relationship between a subscription and a user account. I would like the user account object to be persisted when the subscription is persisted. At first, none of these too exist in the database store.

My mapping looks like:
Code:

    @ManyToOne
    @JoinColumn(name = "user_account_id", nullable = false)
    private UserAccount userAccount;

The integration test looks like:
Code:

    email0 = new Email("mittiprovence@yahoo.se");
    userAccount0 = new UserAccount();
    userAccount0.setFirstname("Stephane");
    userAccount0.setLastname("Eybert");
    userAccount0.setEmail(email0);
    userAccount0.setPassword("toto");
    userAccount0.setPasswordSalt("toto");
    userAccount0.setUnconfirmedEmail(true);
    userAccount0.setLastLogin(new LocalDateTime());
    userAccount0.setImported(false);
    userAccount0.setMailSubscribe(false);
    userAccount0.setSmsSubscribe(false);
    userAccount0.setCreationDatetime(new LocalDateTime());
    elearningSubscription0 = new ElearningSubscription();
    elearningSubscription0.setUserAccount(userAccount0);
    elearningSubscription0.setWatchLive(false);

    elearningSubscription0 = elearningSubscriptionRepository.save(elearningSubscription0);

But I get a failed exception because the user account is not being persisted.

Now, if I add a cascade type attribute, changing the annotation to:
Code:

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_account_id", nullable = false)
    private UserAccount userAccount;

Then the test passes and the user account is being persisted before the subscription.

I thought that the cascading all was a default behavior for a many-to-one relationship.

Is that not the case ?

Any way to set it up as a default behavior ? Or is it not a good thing to do...

I'm using the following stack:
Code:

    <hibernate.version>3.6.10.Final</hibernate.version>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>${hibernate.version}</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>3.5.6-Final</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>1.0.1.Final</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>1.4.0.BUILD-SNAPSHOT</version>
    </dependency>

Kind Regards,

Viewing all articles
Browse latest Browse all 297

Trending Articles