users@glassfish.java.net

Re: How to persist an entity with Composite primary key

From: <glassfish_at_javadesktop.org>
Date: Mon, 25 Jun 2007 22:30:56 PDT

Here is the complete code

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Asset.java == A.java

package entity;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Asset implements Serializable {
        @Id
        @Column(name="Asset_id")
        private int assetId;

        @OneToMany(mappedBy="assetId")
        private Set<Security> securityCollection;

        @OneToMany(mappedBy="assetId")
        private Set<Resources> resourcesCollection;

        private static final long serialVersionUID = 1L;

        public Asset() {
                super();
        }

        public int getAssetId() {
                return this.assetId;
        }

        public void setAssetId(int assetId) {
                this.assetId = assetId;
        }

        public Set<Security> getSecurityCollection() {
                return this.securityCollection;
        }

        public void setSecurityCollection(Set<Security> securityCollection) {
                this.securityCollection = securityCollection;
        }

        public Set<Resources> getResourcesCollection() {
                return this.resourcesCollection;
        }

        public void setResourcesCollection(Set<Resources> resourcesCollection) {
                this.resourcesCollection = resourcesCollection;
        }

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Resources.java == B.java

package entity;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Resources implements Serializable {
        @Id
        @Column(name="Asset_id", insertable=false, updatable=false)
        private int assetId2;

        @Column(name="Resource_name")
        private String resourceName;

        @ManyToOne
        @JoinColumn(name="Asset_id")
        private Asset assetId;

        @OneToMany(mappedBy="res")
        private Set<Security> securityCollection;

        private static final long serialVersionUID = 1L;
        
        public Resources() {
                super();
        }

        public int getAssetId2() {
                return this.assetId2;
        }

        public void setAssetId2(int assetId2) {
                this.assetId2 = assetId2;
        }

        public String getResourceName() {
                return this.resourceName;
        }

        public void setResourceName(String resourceName) {
                this.resourceName = resourceName;
        }

        public Asset getAssetId() {
                return this.assetId;
        }

        public void setAssetId(Asset assetId) {
                this.assetId = assetId;
        }

        public Set<Security> getSecurityCollection() {
                return this.securityCollection;
        }

        public void setSecurityCollection(Set<Security> securityCollection) {
                this.securityCollection = securityCollection;
        }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Security.class == C.class

package entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Security implements Serializable {
        @EmbeddedId
        private Security.PK pk;

        @Column(name="Clearance_Level")
        private int clearanceLevel;

        @ManyToOne
        @JoinColumn(name="Asset_id")
        private Asset assetId;

        @ManyToOne
        private Resources res;

        private static final long serialVersionUID = 1L;

        public Security() {
                super();
        }

        public Security.PK getPk() {
                return this.pk;
        }

        public void setPk(Security.PK pk) {
                this.pk = pk;
        }

        public int getClearanceLevel() {
                return this.clearanceLevel;
        }

        public void setClearanceLevel(int clearanceLevel) {
                this.clearanceLevel = clearanceLevel;
        }

        public Asset getAssetId() {
                return this.assetId;
        }

        public void setAssetId(Asset assetId) {
                this.assetId = assetId;
        }

        public Resources getRes() {
                return this.res;
        }

        public void setRes(Resources res) {
                this.res = res;
        }


        @Embeddable
        public static class PK implements Serializable {
                public int resAssetId;
                public int assetId2;
                private static final long serialVersionUID = 1L;

                public PK() {
                        super();
                }

                public int getResAssetId() {
                        return this.resAssetId;
                }

                public void setResAssetId(int resAssetId) {
                        this.resAssetId = resAssetId;
                }

                public int getAssetId2() {
                        return this.assetId2;
                }

                public void setAssetId2(int assetId2) {
                        this.assetId2 = assetId2;
                }

                @Override
                public boolean equals(Object o) {
                        if (o == this) {
                                return true;
                        }
                        if ( ! (o instanceof PK)) {
                                return false;
                        }
                        PK other = (PK) o;
                        return (this.resAssetId == other.resAssetId)
                                && (this.assetId2 == other.assetId2);
                }

                @Override
                public int hashCode() {
                        return this.resAssetId
                                ^ this.assetId2;
                }

        }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Hint: Resource table inherit form table Asset
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

What is the code which will add to DB an entity of Security

Thank's in advance
[Message sent by forum member 'bakr_awad' (bakr_awad)]

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