Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 3:06 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: secondary table per entity
PostPosted: Sun May 09, 2010 8:25 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time

One entity can have many or one secondary table . This means that the entity is saved across these secondary tables.

One secondary table with primary key for join with the primary table .
Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.SecondaryTable;
import javax.persistence.PrimaryKeyJoinColumn;

@
Entity
@Table(name="PERSON")
@
SecondaryTable(name="PERSON_DETAIL", 
        pkJoinColumns
=@PrimaryKeyJoinColumn(name="PERSON_ID"))

public class Person implements java.io.Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private String Id
;
    private String name;
    private Date birthday;
    private String gender;
    
    
@OneToOne(mappedBy="person") 
    private Bed bed
;
    
    public String getId
() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(name="BIRTHDAT")
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Column(name="GENDER")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setBed(Bed bed) {
    this.bed = bed;
    }
    public Bed getBed() {
    return bed;
    }
 
    
}

 



Multiple secondary table per entity , each one has its own join column .
Code:
@Entity
@Table(name="PERSON")
 @SecondaryTables({
        @SecondaryTable(name="PERSON_DETAIL", 
            pkJoinColumns
=@PrimaryKeyJoinColumn(name="PERSON_ID")),
        @SecondaryTable(name="PERSON_LOG", 
            pkJoinColumns
=@PrimaryKeyJoinColumn(name="PER_ID"))
    })
 



You can specify the referenced join primary key like the following :
Code:
@Entity
@Table(name="PERSON")
@
SecondaryTable(name="PERSON_DETAIL"
        
pkJoinColumns=@PrimaryKeyJoinColumn(name="PERSON_ID",referencedColumnName="ID"))
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 database contents display in table format in swing  GUI  subho07  4
 Creating table using div tag  HTML examples  msi_333  0
 put caption at the bottom of table  HTML examples  msi_333  0
 adding caption to table in html  HTML examples  msi_333  0
 row highlight of table  JavaScript examples  msi_333  0

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team