Total members 11894 |It is currently Thu Nov 21, 2024 10:33 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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"))
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : secondary table per entity
 JPA entity with table generator     -  
 JPA entity class example     -  
 Get all objects for an entity     -  
 cmp entity beans     -  
 Find entity by id     -  
 @EntityListeners in your entity class     -  
 @Embeddable entity and @AttributeOverrides     -  
 @Transient annotation in JPA entity     -  
 create a named query within entity class     -  
 Using table in latex     -  



Topic Tags

Java JPA
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com