Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - How to map one class with multiple tables in Hibernate/javax.persistance?

I want to use one class to map three tables. I know javax.persistance provides the @SecondaryTable annotation to map two tables to one class.

Below is the code, where I have used @SecondaryTable. It allows me to define only one secondary table. But I need 3 tables to be used by the same class.

@Entity
@Table(name = "table1")
@SecondaryTable(name="table2")
public class TableConfig
    implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "mac", table= "table1")
    private String uniqueIdentifier;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I want to use one class to map three tables, From what I know is that javax.persistance provides @SecondaryTable annotation to map two tables to one class

use @SecondaryTables to map more than one table.

You can map a single entity bean to several tables using the @SecondaryTables class level annotations. To express that a column is in a particular table, use the table parameter of @Column or @JoinColumn.


for example there is 3 entity's namely: Name , Address & Student:

Name entity will look like:

@Entity
@Table(name="name")
public class Name implements Serializable {

    @Id
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;

    public Name(){}

    public Name(int id,String name){
        this.id=id;
        this.name=name;
    }
        //getters and setters
}

Address entity will look like:

@Entity
@Table(name="address")
public class Address implements Serializable {

    @Id
    @Column(name="id")
    private int id;
    @Column(name="address")
    private String address;

    public Address(){}

    public Address(int id, String address) {
        super();
        this.id = id;
        this.address = address;
    }
        //getters and setters
}

Student entity will look like:

@Entity
@Table(name="student")
@SecondaryTables({
    @SecondaryTable(name="name", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") }),
    @SecondaryTable(name="address", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") })
})
public class Student implements Serializable {

    @Id
    @Column(name="student_id")
    private int studentId;

    @Column(table="name")
    private String name;

    @Column(table="address")
    private String address;

    public Student(){}

    public Student(int studentId){
        this.studentId=studentId;
    }
        //getters and setters
}

Store like:

    Student s= new Student(1);
    session.save(s);

    Name n=new Name(s.getStudentId(),"Bilal Hasan");
    session.save(n);    

    Address address = new Address(s.getStudentId(), "India");
    session.save(address);

    Student ob = (Student)session.get(Student.class, s.getStudentId());

    System.out.println(ob.getStudentId());
    System.out.println(ob.getName());
    System.out.println(ob.getAddress());

ouput:

1
Bilal Hasan
India

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...