users@glassfish.java.net

One to one integrity is not maintained

From: <glassfish_at_javadesktop.org>
Date: Wed, 02 May 2007 05:01:11 PDT

I have 2 tables in the database with a 1-to-1 relationship as follows:

Project([u]id[/u], name, [appid])
Application([u]id[/u],name)

appid in project is the foreign key to applications id column.

Now, I create two persistence classes Project and Application with following relavant code statements:
Project.java

@Entity
@Table(name = "project")
@SequenceGenerator(name = "proj_id", sequenceName = "project_id_seq")
public final class Project implements java.io.Serializable {
        
        @Id
        @GeneratedValue(generator = "proj_id")
        @Column(name = "id")
        private Integer id;
        ...
       @JoinColumn(name="applicationid", unique=true, nullable=true, updatable=true)
        private Application app;
        ....
        @OneToOne(mappedBy="project")
        @JoinTable(name="application")
        @JoinColumn(name="applicationid", unique=true, nullable=true, updatable=true)
        public Application getApp() {
                return app;
        }
/************** Project.java ends ***********************/

Application.java
@Entity
@Table(name = "application")
@SequenceGenerator(name = "app_id", sequenceName = "application_id_seq")
public final class Application implements java.io.Serializable {

                @Id
                @GeneratedValue(generator = "app_id")
                @Column(name = "id")
                private Integer id;
                ...
/********Application.java ends******/
  
When I attempt to insert multiple projects into the database, it allows me to do that.
My question is, shouldn't it give an exception when it sees that the one-to-one constraints is not followed anymore with duplicate "applications" for multiple "projects"?

If I understood something wrong, could anyone point to me how can I achieve this integrity? Should I do it at the database level?

Regards
Ketan.
[Message sent by forum member 'ketancmaheshwari' (ketancmaheshwari)]

http://forums.java.net/jive/thread.jspa?messageID=215155