openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pbt123 <prabh...@in.ibm.com>
Subject JPA 'OneToMany/ManyToOne' relationship is not detected by OpenJPA at the time of persistence, if no DataBase level constraint defined.
Date Tue, 14 Aug 2012 17:32:51 GMT
Hello Team,

I am new to JPA and facing some problem in entity relationship (referential
integrity). Could any one please help?

JPA 'OneToMany/ManyToOne' relationship(referential integrity) is not
detected by OpenJPA at the time of persistence, if no DataBase level
constraint defined.

Consider two tables, User and UserApp, these do not have back end database
relationship (primary key - Foreign key) instead having JPA relationship
only. And at the time of persistence of child table (UserApp) data,J PA is
not returning error message if parent table(User) data does not exist. 

User :  ID (PK)
UserApp: id (FK) .....linked with User.ID

User table Data:
abc
bcd
cde
def

OpenJPA is allowing UserApp to insert ID="efg".

As per referential integrity rule, UserApp should not be able to insert date
for ID="efg".


Below are the following two entities:


public class User implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        private String id;

       
        //bi-directional many-to-one association to kna1
        @OneToMany( mappedBy="userBean")
        private Set<UserApp> userApps;

        public Set<UserApp> getUserAppss() {
                return this.userApps;
        }

        public void setUserApps(Set<UserApp> userApps) {
                this.userApps = userApps;
        }
       
..
..
..
       




@Table(name="USER_APP",  schema="RDACCESS")
public class UserApp implements Serializable {
        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private UserAppPK id;

    @Temporal( TemporalType.DATE)
        private Date lastlogin;

   
        // bi-directional many-to-one association to User
        @ManyToOne( optional=false )
        @JoinColumns({ @JoinColumn(name = "ID", referencedColumnName =
"ID")})
        private User userBean;

       
       
       
        public User getUserBean() {
                return this.userBean;
        }

        public void setUserBean(User userBean) {
                this.userBean = userBean;
        }
..
..
..


@Embeddable
public class UserAppPK implements Serializable {
        //default serial version id, required for serializable classes.
        private static final long serialVersionUID = 1L;

        private String id;

        private String appname;

    public UserAppPK() {
    }
        public String getId() {
                return this.id;
        }
        public void setId(String id) {
                this.id = id;
        }
        public String getAppname() {
                return this.appname;
        }
        public void setAppname(String appname) {
                this.appname = appname;
        }


...
...
...


You can check, these two tables are linked with field "ID".

The problem is, Application is allowing to insert UserApp without User's
"ID" .


Code which inserts value in UserApp:


                    UserApp userApp ;
                    UserAppPK id;

                    UserAppManager uAppMgr=new
UserAppManager(JpaManager.getCurrentSession()
                    userApp.setLastlogin(convertedDate);
                    id.setAppname(p_appName);
             id.setId(p_LoginUser);
                    userApp.setId(id);
                    uAppMgr.createUserApp(userApp);

..
..
..




        @Action(Action.ACTION_TYPE.CREATE)
        public String createUserApp(UserApp userApp) throws Exception {
                EntityManager em = getEntityManager();
                try {
                        em.getTransaction().begin();
                        em.persist(userApp);
                        em.getTransaction().commit();
..
..
..


I may be missing some configuration or the way of implementation. 



--
View this message in context: http://openjpa.208410.n2.nabble.com/JPA-OneToMany-ManyToOne-relationship-is-not-detected-by-OpenJPA-at-the-time-of-persistence-if-no-Dat-tp7580878.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Mime
View raw message