Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i map multiple classes to one table in hibernate?

Tags:

java

hibernate

From my research it seems not likely that its possible, but here is my use case.

I have a table called user with an address id.

I'd like to map a UserReference and User class to this table where UserReference does not contain the Address object (to save sql joining time) and User does contain the Address object (in case it's needed).

I can't use since it expects a join table, and I can't just define two separate classes because a get() seems to return double of every row (one for User, one for UserReference).

Does anyone know how I could go about this?


I guess I should try to explain myself better.

public class UserReference {
    private int id;
    private String name;
}

public class User extends UserReference {
    private Address;
}

I'd like to map both UserReference and User so that I can query for UserReference when I only need the basic details, and User whenever I need the full object.

I understand that I can simply have Address be lazy loaded but I have a two tier system which unproxies all objects once it passes through the layers.


edit:

Perhaps my question was too ambigious but based on the answers it doesn't seem very possible to do what I want. Thanks anyway everyone.

like image 685
kennyg Avatar asked Apr 15 '15 16:04

kennyg


People also ask

How can we map our classes to database tables in Hibernate?

The <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.

How do you map an entity to multiple tables in Hibernate?

Solution: Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

What is one to many and many to one relationship in Hibernate?

One To Many Mapping in Hibernate. In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping.


2 Answers

Your question is not very clear to me but I'll try to answer as per my understanding. It depends upon what you really want to persist and what you want as a java entity.

Consider you want to represent User class with a database table i.e. represent User as entity in JPA. Now you can Create an Embeddable class which will be part of another class.

So you can map an Address class in User Entity and make the Address class as Embeddable using @Embeddable annotation package com.thejavageek.embeddabledemo;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

    private String area;
    private String city;
    private String pincode;

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

}

and you can embed this embeddable class as below

package com.thejavageek.embeddabledemo;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;

@Entity
public class Person {

    @TableGenerator(name = "person_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", allocationSize = 100)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "person_gen")
    private String idperson;
    private String name;

    @Embedded
    private Address address;

    public String getIdperson() {
        return idperson;
    }

    public void setIdperson(String idperson) {
        this.idperson = idperson;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}
like image 111
Prasad Kharkar Avatar answered Sep 22 '22 15:09

Prasad Kharkar


UserReference.java

@Entity
@DiscriminatorColumn(name = "type")
@DiscriminatorValue(value = "UserReference")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class UserReference extends BaseEntity {

    private String name;
}

User.java

@Entity
@DiscriminatorValue(value = "User")
public class User extends UserReference {
    @OneToOne
    @JoinColumn(name = "address_id")
    private Address address;

}
like image 37
Bartun Avatar answered Sep 22 '22 15:09

Bartun