I want to create a primary composite key and use an #Id field from a parent class. But it does not work. Why?
#MappedSuperclass
static abstract class SuperEntity {
#Id
private Long id;
}
#Entity
#IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
#Id
private String lang;
}
public class SuperPK {
public SuperPK(Long id, String lang) {
//...
}
}
Result: Property of #IdClass not found in entity ChildEntity: id
I found an open issue regarding this bug.
One of the comments states to override the getters for the ID properties as a workaround.
#Entity
#IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
#Id
private String lang;
#Override #Id
public Long getId() {
return super.getId();
}
}
Related
i'm using a library which have some entity and relations
like this :
public class BaseEntity1{
private Long id;
private List<BaseEntity2> baseEntity2List;
}
public class BaseEntity2{
private Long id;
private String title;
}
Entities have no jpa annotation's
i need to persist them into database
so i extend them and Override getter's to add jpa annotations
like this :
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ChildEntity1 extends BaseEntity1{
#Override
#Id
public Long getId(){
..
}
#Override
#OneToMany
public List<BaseEntity2> getBaseEntity2List(){
..
}
}
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ChildEntity2 extends BaseEntity2{
#Override
#Id
public Long getId(){
..
}
#Override
#Column
public String getName(){
..
}
}
my problem is that OneToMany relation , i have no idea to replace BaseEntity2 to ChildEntity2
Does anyone have any idea ?
I created abstract generic #MappedSuperclass. This is working fine with select, insert queries. But hibernate don't generate update queries. I don't understand what's wrong with generic class.
#MappedSuperclass
#Cacheable
public abstract class Category<T extends Category> {
private Integer id;
private String name;
private T parent;
private List<T> children;
#Id #GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
//omitted
}
#Entity
#Table(name = "staticPageCategory")
public class StaticPageCategory extends Category<StaticPageCategory> implements Serializable {
#ManyToOne
#Override
public StaticPageCategory getParent() {
return super.getParent();
}
#OrderBy("ordering asc, name asc")
#OneToMany(mappedBy = "parent")
#Override
public List<StaticPageCategory> getChildren() {
return super.getChildren();
}
}
Question is why hibernate ignoring getCurrentSession().update(myCategory);
I have a Class system
#Entity
abstract class System{
#Id
int systemId;
//setter and getters..
}
and which is extended by class
#Entity
class PhysicalSystem extends System
{
#Id
int place;
//setter and getters..
}
in need to make the composite key by using the systemId and place
how can i do this.. if i have #Id in both class its throws exception
Initial SessionFactory creation failed.java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass
How can i solve this?
Tables:
System{
systemid PK
systemName
}
PhysicalSystem
{
systemId PK
locationId PK
}
In your case, maybe the best solution is a OneToOne mapping:
#Entity
public class PhysicalSystem implements Serializable {
#EmbeddedId
private PhysicalSystemKey key;
#JoinColumns({JoinColumn(name = "key.systemId", referencedColumnName = "ctvId"})
#OneToOne(mappedBy = "physicalSystem")
private System system;
// ...
}
#Embeddable
public class PhysicalSystemKey {
private Long systemId;
private Long locationId;
// ...
}
#Entity
public class System implements Serializable {
#Id
private Long systemId;
#OneToOne(mappedBy = "system")
private PhysicalSystem physicalSystem;
}
I'm working with Hibernate Annotations and the issue that I'm trying to solve goes as follows:
I need to have 2 different #Entity classes with the same columns mapping but with a different Identifier.
The first one should use id as identifier.
The second should use name as identifier.
So, I have an abstract class, annotated with #MappedSuperclass that have all of the columns including id and name, and in addition 2 #Entity classes that extends the super class and overriding the getters of the id and name.
#MappedSuperclass
public class MappingBase {
protected Integer id;
protected String name;
#Column (name = "ID")
public void getId() {
return this.id;
}
#Column (name = "NAME")
public void getName() {
return this.name;
}
}
#Entity
#Table (name = "TABLE")
public class Entity1 extends MappingBase {
#Id
#Column (name = "ID")
public void getId() {
return this.id;
}
}
#Entity
#Table (name = "TABLE")
public class Entity2 extends MappingBase {
#Id
#Column (name = "NAME")
public void getName() {
return this.name;
}
}
Note: I must have the members (id,name) in the super class.
I know that i can add #Transient to the id and name getters but this means that i must add both of them in each class and it's not a good design :(
In addition, the following insertable="false, updateable=false can help but i don't understand what is the meaning of this...
Please help me!
Hibernate/JPA allows us to annotate either properties or accessors. If we have #Id annotation on a property, JPA will lookup all the properties of the class. Similarly, if we have #id annotation on a getter method, JPA will lookup all the getters.
We can solve the above problem by annotating properties instead. The superclass and the two subclasses will be as follows-
#MappedSuperclass
public abstract class AbstractMappingBase {
//properties other than id and name
public abstract Integer getId();
public abstract String getName();
//other getters and setters
}
#Entity
public class Entity1 extends AbstractMappingBase {
#Id
private Integer id;
private String name;
#Override
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#Entity
public class Entity2 extends AbstractMappingBase {
private Integer id;
#Id
private String name;
#Override
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here JPA will look for properties instead of getters. There are no duplicate properties between superclass and its subclasses. So it will work fine.
You are much better off defining your base class as #Embeddable and using #Embedded in your implementation classes with the use of #AttributeOverride.
If i remember correctly, I simply defined 2 #Entity classes with the same table that inherits from one abstract #MappedSuperclass class. The super class contains the id member and each Entity class define it's own #Id #Column definition. It should work!
In my application I am trying to implement Hibernate mappings through annotation. There I have a base class which is abstract, in this class the Id attribute is present. I am inheriting this base class with a child class. The code is given below:
#MappedSuperclass
#Inheritance(strategy=InheritanceType.JOINED)
public abstract class Base implements IBase {
private static final long serialVersionUID = -1433573674276444516L;
private int id;
public Base() {
}
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
And the child class is:
#Entity
#Table(name="USER")
public class User extends Base implements IUser {
private static final long serialVersionUID = 344528694909088439L;
private String name;
public User() {
}
#Column(name="NAME", nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
It is creating the USER Table and working fine.
I was wondering whether or not I am doing it in right way.
Thanks.
If the goal is just to have several independant entities to inherit a common field from a base class, then no, you're not doing it correctly. The annotation #Inheritance is unnecessary. #Inheritance is necessary when you have an entity (Vehicle, for example), and several sub-entities (Car, Bike, for example).