Home Reusing entities in JPA/Spring Boot
Reply: 0

Reusing entities in JPA/Spring Boot

user1508
1#
user1508 Published in May 21, 2018, 11:01 am

I have an existing entity that cannot be modified as below:

@Entity
public class ChildEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="id")
private String id;
@Column(name="name")
private String name;
//getters, setters, et al.

I have a new query that returns the same data as above with an extra column as below:

@Entity
public class ParentEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name="extra")
private String extra;
@Id
@Column(name="id")
private String id;
@Column(name="name")
private String name;
//getters, setters, et al. from child

As visible above, I want to reduce the above class to something like this:

@Entity
public class ParentEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name="extra")
private String extra;
@Embedded
private ChildEntity entity; //ChildEntity will have annotation @Embeddable
//getters, setters, of extra param

But when I run the native query that assigns data to a list of ParentEntity, it does not recognize ChildEntity.

My queries would be something like this:

ChildEntity: select name,id from table where (/*some condition*/)
ParentEntity: select extra,name,id from table where (/*some condition*/)

How do I do this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305048 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO