0

My DB schema has two tables :- Rule Table and Rule Scope table.
Rule Table columns (rule_id Primary Key, .....) and Rule Scope columns (rule_id Foreign key , Scope_id (NOT Auto generated Id, Can repeat for different rule_id)
Rule Scope Primary key is combination of rule_id and Scope_id.

My RULE Entity

@Entity
@Table(name = "RULE")
public class Rule implements IEntity {

@Column(name = "RULE_ID")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int ruleId;

@OneToMany(fetch=FetchType.LAZY,mappedBy="rule")
private Set<RuleScope> ruleScope=new HashSet<RuleScope>();  

Rule Scope Entity:-

@Entity
@Table(name = "RULE_SCOPE")
public class RuleScope {

@Embeddable
public static class Id implements Serializable{
    @Column(name = "RULE_ID")
    private int ruleId;
    @Column(name = "SCOPE_ID")
    private int scopeId;
}

@EmbeddedId
private Id id = new Id();

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "RULE_ID", insertable = false, updatable = false)
private Rule rule; 

I have following questions:-
When I am trying to save the Rule , but it is not persisting the Child Entity. it is giving 0 for Rule Id when persisting the Rule Scope which is violating the Foreign Key Constraint. But when i use @JoinColumn the it is working. Please help me to persist the Rule Scope while persisting Rule and using mappedBy.

Art
  • 414
  • 7
  • 24
  • Have you tried to: @OneToMany(mappedBy="rule", fetch=FetchType.LAZY) private Set ruleScope=new HashSet(); – Jama Djafarov Dec 01 '14 at 16:13
  • It is giving the same exception – Art Dec 01 '14 at 16:55
  • I am currently using OpenJPA, and cannot be very helpful. I would try ManyToMany, PrimaryKeyJoinColum, or JOIN Strategy.... You can review later, and change it back. – Jama Djafarov Dec 01 '14 at 22:00
  • @JamaDjafarov... I am able to resolve the issue noww...and its working but now when i am using mappedBy it is not persisting the Child entities(searched one net it shows that that parent is not the owner) but if i use @JoinColumn it is persisting both Parent and Child. Can you please clarify as how to get the parent and child persisted when using `mappedBy` – Art Dec 02 '14 at 06:50
  • I would say, try in RULE: @OneToMany(cascade = CascadeType.ALL,mappedBy="rule") . Also, if it doesn't work, try a different approach. I noticed when I try a different approach, in most cases it helps to find the issue. – Jama Djafarov Dec 02 '14 at 14:47
  • @JamaDjafarov i have already tried tht Cascading, it is not persisting , everytime it is giving newly created Parent entity Id as 0. What is the other approach you are suggesting? Is it of that helper functions? – Art Dec 02 '14 at 18:16
  • try ManyToMany, or you can try JOIN Strategy - class RuleScope extends Rule – Jama Djafarov Dec 02 '14 at 21:53

1 Answers1

1

Try this:

Rule Entity:

@Entity
@Table(name = "RULE")
public class Rule implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "RULE_ID", unique = true, nullable = false)
    private int ruleId;

    @OneToMany(mappedBy = "id.rule", cascade = CascadeType.ALL)
    private Set<RuleScope> ruleScope = new HashSet<RuleScope>(0);

    // Generate Getters, Setters, hashCode() and equals()
}

RULE_SCOPE Entity:

@Entity
@Table(name = "RULE_SCOPE")
public class RuleScope implements Serializable {

    @EmbeddedId
    private Id id;

    // Generate Getters, Setters
}

RULE_SCOPE composite PK:

@Embeddable
public class Id implements Serializable {

    @ManyToOne
    @JoinColumn(name = "RULE_ID", , nullable = false)
    private Rule rule;

    @Column(name = "SCOPE_ID")
    private int scopeId;

    // Generate Getters, Setters, hashCode() and equals()
}
Chazar
  • 81
  • 3
  • thanks for the update..i corrected and it started working. I used a different approach.. – Art Dec 07 '14 at 16:27