[jboss-user] [EJB 3.0] - Re: Foreign key error

waynebaylor do-not-reply at jboss.com
Tue Aug 14 15:34:21 EDT 2007


i just put together this example which mirrors your entities, and i was able to save without FK errors.

Entity Code:
@Entity
  | @SequenceGenerator(name="child_seq", sequenceName="child_seq")
  | public class Child
  | {
  |     
  |     private Long id;
  |     
  |     private Parent parent;
  |     
  |     private String name;
  | 
  |     public Child(){}
  |     
  |     public Child(String name)
  |     {
  |         this.name = name;
  |     }
  |     
  |     @Id
  |     @GeneratedValue(strategy=GenerationType.AUTO, generator = "child_seq")
  |     public Long getId()
  |     {
  |         return id;
  |     }
  | 
  |     public void setId(Long id)
  |     {
  |         this.id=id;
  |     }
  | 
  |     public String getName()
  |     {
  |         return name;
  |     }
  | 
  |     public void setName(String name)
  |     {
  |         this.name=name;
  |     }
  | 
  |     @ManyToOne(cascade=CascadeType.ALL)
  |     @JoinColumn(name="parentId", referencedColumnName="id")
  |     public Parent getParent()
  |     {
  |         return parent;
  |     }
  | 
  |     public void setParent(Parent parent)
  |     {
  |         this.parent=parent;
  |     }
  |     
  | }
  | ------------------------
  | @Entity
  | @SequenceGenerator(name="parent_seq", sequenceName="parent_seq")
  | public class Parent
  | {
  |    
  |     private Long id;
  |     
  |     @Id
  |     @GeneratedValue(strategy=GenerationType.AUTO, generator = "parent_seq")
  |     public Long getId()
  |     {
  |         return id;
  |     }
  | 
  |     public void setId(Long id)
  |     {
  |         this.id=id;
  |     }
  | 
  | }
The SLSB I used to create/save is:
@Stateless
  | @Local(TesterLocal.class)
  | @LocalBinding(jndiBinding="session.Tester/local")
  | public class Tester implements TesterLocal
  | {
  |     @PersistenceContext(unitName="PU2")
  |     private EntityManager em;
  |     
  |     public void doWork()
  |     {
  |         for(int j=0; j<5; ++j)
  |         {
  |             Parent p = new Parent();
  | 
  |             for(int i=0; i<5; ++i)
  |             {
  |                 Child c = new Child("child-"+i);
  |                 c.setParent(p);
  | 
  |                 em.persist(c);
  |             }
  |             em.flush();
  |         }
  |     }
  | }

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074157#4074157

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074157



More information about the jboss-user mailing list