Joe Wilson
Joe Wilson

Reputation: 89

@ManyToOne as reference to other table

I'd like the security question in the account entity to "reference" one of the security questions in the SecurityQuestion table and not create a new one. Take the following:

@Entity
public class Account {

    @Id
    @GeneratedValue...
    private Long id;
    @ManyToOne(cascade=CascadeType.ALL)
    private SecurityQuestion question;
    ...
}

@Entity
public class SecurityQuestion {

    @Id
    @GeneratedValue...
    private Long id;
    private String question;
    ...
}

When using Spring's form:select to select the security question for the account, it creates a new security question when I save the account entity. Note: The SecurityQuestion table is a list of predefined questions to display.

<form:select path="securityQuestion.question">
    <form:option value="" label="Select..."/>
    <form:options items="${questions}" />
</form:select>

How can this be accomplished?

Upvotes: 0

Views: 75

Answers (1)

Michail Nikolaev
Michail Nikolaev

Reputation: 3775

I think it is because CascadeType.ALL set and you lose somewhere id of SecurityQuestion (so, hibernate in order to CascadeType.PERSIST creates new entity).

Upvotes: 0

Related Questions