4

I'm wondering if there's a possibility to fetch multiple tables to only one java class for exemple :

TABLE LABELS;
TABLE STANDARDS;
TABLE REFERENCES;

mapped to the same class

public Class Information {

    private String type; // the type is the element who have to do the mapping => LABELS/STANDARDS/REFERENCES
    ...
}

It's not possible for me to construct one class for each type for technical reason (I known that some heritage should be cool).

Thank you

Gilles

EDIT :

I'll try to expain a bit more :)

I'm using a JMS service to get the informations. Each message have a particulary type, (in my exemples : "labels","standards" and "references").

By using those type, I want to persit the informations in the respective Tables. The structure is exactly the same for every messages, it's why I wanna use a unique POJO.

I hope it was better explain :)

EDIT 2 :

TABLE LABELS (
    ID PRIMARY KEY AUTO_INCREMENT,
    MESSAGE VARCHAR(255),
    AUTHOR VARCHAR(255)
);
TABLE STANDARDS(
    ID PRIMARY KEY AUTO_INCREMENT,
    MESSAGE VARCHAR(255),
    AUTHOR VARCHAR(255)
);
TABLE REFERENCES (
    ID PRIMARY KEY AUTO_INCREMENT,
    MESSAGE VARCHAR(255),
    AUTHOR VARCHAR(255)
);

and here's some examples of JMS

headers : 
    type : label
body:
    {message:"name of the country",author:"john doe"}

headers : 
    type : label
body:
    {message:"nom du pays",author:"jenny doe"}

headers : 
    type : reference
body:
    {message:"country",author:"john doe"}

and I want to put them into the Information Class and persist them into the correct Table

Gilles Bodart
  • 594
  • 1
  • 10
  • 27

3 Answers3

1

Try this:

 @MappedSuperclass
 public class Base {
   private String message;
   private String autor;
   @Column(name = "MESSAGE")
   public String getMessage(){
     return message;
   }
   public void setMessage(final String message) {
     this.message = message;
   }
   @Column(name = "AUTOR")
   public String getAutor(){
     return autor;
   }
   public void setAutor(final String autor) {
     this.autor = autor;
   }
 }

And three classes:

 @Entity
 @Table(name="LABELS")
 public class Labels extends Base{};

And

 @Entity
 @Table(name="STANDARDS")
 public class Standards extends Base{};

And

 @Entity
 @Table(name="REFERENCES")
 public class References extends Base{};

Now you can persist the data using:

 Base b;
 if (info.getType().equals("REFERENCES")) {
    b=new References();
 } else if (info.getType().equals("LABELS")) {
    b=new Labels();
 } else if (info.getType().equals("STANDARDS")) {
    b=new Standards();
 } else {
   return;
 }
 b.setMessage(info.getMessage());
 b.setAutor(info.getAutor());
 Transaction t = session.beginTransaction();
 session.persist(b);
 t.commit();
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
Grim
  • 1,938
  • 10
  • 56
  • 123
  • I was trying to do that now, I going back to you if it works :) I have to abandon the idea to have a beautiful unique POJO :'( – Gilles Bodart Dec 14 '15 at 15:31
  • Probably the correct solution but note that class Base should be annotated with the @MappedSuperclass annotation. – Alan Hay Dec 14 '15 at 15:49
  • @AlanHay What's the difference if the class isn't annotated with this MappedSuperclass ? – Gilles Bodart Dec 14 '15 at 15:58
  • @AlanHay and must I define the named queries for each subEntity ? or can I put it into the parent Entity ? – Gilles Bodart Dec 14 '15 at 16:02
  • 1
    You do not have a parent **Entity**. You have a mapped superclass defining common mappings. Because it is not an Entity i.e. has no representation in the database you cannot query a mapped superclass. While you can define Named Queries on a Mapped Superclass, these are probably not going to work as you expect: http://stackoverflow.com/a/16426513/1356423 – Alan Hay Dec 14 '15 at 16:06
  • @AlanHay thanks for the post, I see know a bit more how it works. Is it possible to do this configuration on a XML config file ? – Gilles Bodart Dec 15 '15 at 08:30
  • @PeterRader Because I have no choice :D :'( – Gilles Bodart Dec 15 '15 at 09:04
  • @AlanHay I accept the edit but disagree with the explaintation. The difference between the Use `@MappedSuperclass` and not use `@MappedSuperclass` is: Are the Tables real inherited or not! If the table in the database are inherited from a base-table avoid the `@MappedSuperclass`. From the docu: **A mapped superclass has no separate table defined for it.** – Grim Dec 15 '15 at 09:17
  • @PeterRader My boss want be able to change the mapping from a project to another by keeping the POJOs. So I have to do the mapping in xml to be as far from the mapping as I can ... – Gilles Bodart Dec 15 '15 at 09:40
  • @PeterRader Don't worry I already did it ;-) do you want me to put a pseudo solution ? – Gilles Bodart Dec 15 '15 at 09:54
0

You can use secondaryTables:

http://docs.oracle.com/javaee/6/api/javax/persistence/SecondaryTables.html

OPK
  • 4,120
  • 6
  • 36
  • 66
  • Wasn't it JPA and not Hibernate ? It's mandatory for me to use Hibernat I don't have the choice of that ... :/ – Gilles Bodart Dec 14 '15 at 14:28
  • SecondaryTables are for some part information of a class mapped in multiple Table. In my case the whole class have to be mapped :/ – Gilles Bodart Dec 14 '15 at 14:30
  • 1
    @GillesBodart JPA is just an specification, in other words its just a guideline. Hibernate is the implementation,which provides functionality. – OPK Dec 14 '15 at 14:31
  • 2
    Secondary Tables are of no use in this scenario as there is no relationship between the tables. – Alan Hay Dec 14 '15 at 15:50
0

You might be asking if you can do a table per subclass mapping. You can have one class per table, but all inherit from a common base class.

Alternately, if you're building a read-only mapping, you could make a view that is a UNION over all the relevant tables, and just map the view using Hibernate. Something like

-- SQL
CREATE VIEW information AS 
  SELECT 'LABEL' AS type,... FROM labels
  UNION
  SELECT 'STANDARD',... FROM standards
  UNION 
  SELECT 'REFERENCE',... FROM references
  ;

/* Java */
@Entity
@Table(name="information")
public class Information {
   ...
}
david
  • 997
  • 6
  • 15