class A implements Serializable{
private static final long serialVersionUID = 1L;
#Id
Integer id;
...
// constructor getter and setter
}
#Entity
class B extends A{
private static final long serialVersionUID = 1L;
#Column
String name;
#Column
String age;
...
//constructors, getters and setters
}
As you see above, class A extends from class B, B should have the identifier id inheritance from A. but i got the No identifier specified for entity: com.d.e.B
what did i missed? thanks in advance.
You missed the #MappedSuperclass annotation on A, to tell Hibernate/JPA that properties and JPA annotations found in A must be considered.
In this example, #MapperSuperclass will work.
If you have entity relationships (one to many, many to one, etc...) the A class should be defined as an entity and the #Inheritance annotation should be used instead.
I had the same error ("No identifier specified for entity") because my A class had the #Inheritance annotation but not the #Entity annotation
Related
I have this generic entity:
#MappedSuperclass
abstract class Position<T> {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Enumerated(EnumType.STRING)
private T name;
}
But there's a case where the generic type is a String:
#Entity
class ChildPosition0 extends Position<String> {
}
And, JPA will complaint that String is not an enum in this case, but I need to annotate this name field if it's an enum, if not, the database will mark it as int type, and that's not ideal. How do I solve this? How to annotate the field conditionally?
My workaround:
Use Position as a parent class, and adding those field in child class individually, even though they share the same field:
#MappedSuperclass
abstract class Position {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
And extends it from child entity like this:
Child1:
#Entity
public class ChildPosition1 extends Position {
#Enumerated(EnumType.STRING)
private Priority name; // <- Priority is enum type
}
Child2:
#Entity
public class ChildPosition2 extends Position {
private String name;
}
This is too ugly IMO. And Java does not allow class field override from child class. So, back to the question: how to annotate generic field conditionally?
I have an #Entity class, with an #Id annotation and a #OneToOne annotation on the same field. Usually this would not be a problem, but the entity class of the field with these annotations uses a composite key. This is causing more complications than I anticipated.
Here is the entity class that is posing the problem:
#Entity
public class ReportDetails implements Serializable {
#Id
#OneToOne
private MachineLine machineLine;
}
And here is the MachineLine entity class that is being used as an ID in ReportDetails:
#Entity
#IdClass(MachineLine.MachineLineKey.class)
public class MachineLine {
#Id
#ManyToOne
private Machine machine;
#Id
private long lineIndex;
public static class MachineLineKey implements Serializable {
private Machine machine;
private long lineIndex;
}
}
I have left out any extra fields and the getters and setters from these class definitions, to save space.
When I try to run my application it gives the following exception:
java.lang.IllegalArgumentException: This class [class ReportDetails] does not define an IdClass
When I put an #IdClass annotation on ReportDetails it then requires defining the individual fields of whatever class I define in #IdClass, like in MachineLine. However, I am trying to avoid doing this, in favour of having the whole MachineLine entity returned whenever a ReportDetails entity is retrieved from the database.
Is there a way of having MachineLine as the ID field of ReportDetails, without having to define extra fields within ReportDetails?
This is what JPA calls a "derived identity". You might try something like this:
ReportDetails:
#Entity
public class ReportDetails implements Serializable {
// all attributes map by the relationship: AttributeOverride is not allowed
#EmbeddedId
private MachineLine.Id id;
#MapsId
#JoinColumns({
#JoinColumn(name="machineId", referencedColumnName="machineId"),
#JoinColumn(name="machineLineIndex", referencedColumnName="index")
})
#OneToOne
private MachineLine machineLine;
// ...
}
MachineLine:
#Entity
public class MachineLine {
#EmbeddedId
private Id id;
#MapsId("machineId") // maps machineId attribute of embedded id
#ManyToOne
private Machine machine;
// ...
#Embeddable
public static class Id implements Serializable {
private long machineId; // corresponds to PK type of Machine
private long index;
// ...
}
}
Machine:
#Entity
public class Machine {
#Id
private long id;
#OneToMany(mappedBy = "machine")
private List<MachineLine> lines;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
I have some common fields in every table of Database
added_on and added_by
I created a pojo class:
public class CommonBean{
#Column(name="added_on")
public Date addedOn;
#Column(name="added_by")
public Integer addedBy;
//setters and getters
}
And all the pojo classes are extends that pojo class:
example:
#Table(name = "employee")
#Entity
public class HrEmployee extends CommonBean implements java.io.Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
#Column(name="first_name")
private String firstName;
#Column(name="middle_name")
private String middleName;
#Column(name="last_name")
private String lastName;
}
But When I am calling list method of hibernate criteria.
I can see the generated query in the console:
Hibernate:
/* criteria query */ select
this_.id as y0_,
this_.first_name as y2_,
this_.middle_name as y3_,
this_.last_name as y4_
from
hr_employee this_
Why it is not getting attributes from its parent class?
I am not sure whether it is possible or I am making mistake somewhere.
Thanks
Annotate CommonBean class with #MappedSuperclass annotation
You need to Annotate Super class with #MappedSuperclass. this is how you say hibernate to inherit properties from super class
this will help you
I'd like to map lots of subclasses with a common parent class : B extends A, C extends A,... but the database doesn't care A. And B and C have nothing in common (no Id, no reference...).
public class A {
#Id
#Column(name="id")
private Long id;
#Column(name="reference", nullable=false)
private String reference;
}
Is it possible to do this without adding #Entity ?
SOLUTION
#MappedSuperclass
public class A {
#Id
#Column(name="id")
private Long id;
#Column(name="reference", nullable=false)
private String reference;
}
#Entity
#Table(name="B")
public class B extends A {
}
Use #MappedSuperclass on class A:
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.
I'm trying to duplicate something you can do in .Net but not having much luck.
Is the following not possible in Java or am I just missing something? When I run it I get told there is no identifier specified for entity Group.
public abstract class RCEntity
{
#Id #GeneratedValue
private int id;
//getters & setters
}
#Entity
public class Group extends RCEntity {
}
Add the annotation #MappedSuperclass to your super class, i.e.
#MappedSuperclass
public abstract class RCEntity
{
#Id #GeneratedValue
private int id;
//getters & setters
}
From this section in the docs:
Any class in the hierarchy non annotated with #MappedSuperclass nor #Entity will be ignored.