-1

A Table: NAME (varchar2), AGE (varchar2), ID_NO (varchar2)

B Table: NAME (varchar2), ADDRESS (varchar2)

And I got entity like this

@Entity
public calss A {
    @Column(name="NAME")
    private String name;
    @Column(name="AGE")
    private String age;
    @Column(name="ID_NO")
    private String idNo;
    @Column(name="ADDRESS")
    private String address;
}

When I excute this code:

session.createsqlQuery("SELECT * FROM A ").addEntity(A.clss).uniqueResult();

I got SQLException means address column not exists. And I don't want to put the @Transient to Address column, because I hope that I can use this entity to use B Table like this:

session.createsqlQuery("SELECT * FROM B ").addEntity(A.clss).uniqueResult();

How can I do to get this result successfuly?

kayess
  • 3,384
  • 9
  • 28
  • 45
  • Why not creating class `B`, that extends `A` and move field address on `B`? – Albert Bos Sep 08 '17 at 10:53
  • You have 2 tables and an entity that represents neither table. What you need is a tutorial. Maybe even several tutorials. – Kayaman Sep 08 '17 at 10:55
  • I don't think that is allowed in Hibernate. You can check the answers [here](https://stackoverflow.com/questions/3505866/how-to-map-one-class-to-different-tables-using-hibernate-jpa-annotations). – Procrastinator Sep 08 '17 at 10:56
  • @AlbertBos u're right , entends is the only way i can do, thanks~ – C. Eason Sep 11 '17 at 02:01

1 Answers1

0

A short answer: you can't.

If you want to have this kind of things you should create class hierarchy (minimum without @Inheritance and other things I expect you to google on your own):

@Entity
public calss A {
    @Column(name="NAME")
    private String name;
    @Column(name="AGE")
    private String age;
    @Column(name="ID_NO")
    private String idNo;
}

@Entity
public class B extends A {
    @Column(name="ADDRESS")
    private String address;
}

However, it won't work either as you should specify @Id column.

Dmitry Senkovich
  • 5,521
  • 8
  • 37
  • 74
  • At first,I try to not generate lot of entity of tables,but it seems unavoidable – C. Eason Sep 11 '17 at 02:11
  • @C.Eason yes, it is. But what the purpose of this? By default you will have single table hierarchy so there is no need to create one more table. I don't see any other potentional doubts – Dmitry Senkovich Sep 11 '17 at 07:56
  • In my old project , there are many complex sql ,they don't use hibernate there, Now i want change query method to hibernate , but sql has join a lot table or too many subquery ...Can u give me some advice about handle complex sql ? – C. Eason Sep 12 '17 at 01:56
  • @C. Eason the main advice that come to my mind is to be wise with joins then. beware things like `size(collection_name)` and all this stuff as it results in subqueries and can be performance cost. take a look [here](https://stackoverflow.com/a/3760901/5185646). also be careful with eager/lazy collections fetching as you can suddenly fetch entire collection that or forget to trigger lazy collection fetching. I guess mostly that's it that comes to my mind – Dmitry Senkovich Sep 12 '17 at 08:44