I need to migrate some hibernate hbm files to JPA/Hibernate annotations.
The existing relationship is as follows -
The parent class has an ID
The component class also has an ID
The 2 identifiers refer to different sequences.
I have used #Embedded and #AttributeOverride in the parent class, and #Embeddable in the component class.
Both the classes are entities.
The exception i get when i try to save a parent class object is -
org.hibernate.MappingException: component property not found: id
I suppose the exception is because i have 2 identifiers defined.
Any suggestions/workarounds on this will help greatly.
You can't make something an #Entity and #Embeddable at the same time, that makes no sense. You have to make it one or the other.
If both have an ID, and both are entities, then the Hibernate/JPA component/embeddable model doesn't apply.
Related
I was thinking that I understand the relations. But now.. Im not sure. I have a problem to add correct annotations.
I have classes Members and Relations.
In Relations
private GT_Member Mother;
private GT_Member Father;
private List<GT_Member> children;
One Member can be in several Relations as Mother or Father (reference to his sex) but he can be only in One relation as child.
I thought annotate Mother and Father as #OneToMany.
But I'm not sure if I can annotate List as #OneToOne ??
This seems like a problem in modeling the correct entity-relationship model for your database schema and visualizing your ORM (Object Relationship Model).
Rather than starting with classes Members and Relations, please first see what are the dominant data-entities in your system. And how would they be related to each other.
Personally I do not think Relation would be a good JPA entity.
Member looks more like a good entity and could embody the relations
Assuming one Father and one Mother, One to Many seems wrong but as a father or mother can have many children, the correct annotation should be #ManyToOne.
Children is definitely OneToMany, and yes you can annotate the List children as #OneToMany.
Member could have the following properties:
#Entity
public class Member implements Serializable{
#ManyToOne
private Member mother;
#ManyToOne
private Member father;
#OnetoMany
private List<Member> children;
}
This solves both your use cases and in this simple example Relation class is not needed.
hope this helps.
Employment of Relation entity is because I want to save information about status of relation. Donc I will store information about all married etc. Entity relation has other fields like type (neutral, married, fiance etc...).
I have an entity named Client, And it has some named queries and native named queries. What I want to do is, I want to move this named queries to another class. For that I would like to extend the Client entity by another class ClientQuery. And move all named,native queries to that class. Is it possible to do so?
Client CLASS
#XmlRootElement(name = "CLIENT_DETAILS")
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#NamedQueries({
#NamedQuery(name = Client.GET_CLIENT_BYLANGID,
query = "select T from Client T where T.clientPK.langId=:langId")
})
public class Client implements Serializable {
public static final String GET_CLIENT_BYLANGID = "Client.getClientByLangId";
As I understand, you want to know whether it correct to move the #NamedQuery out of the Entityclass to a non-entity class.
I have quickly checked the specification and did not see any restrictions about that. Additionally I have tried to put in an mapping.xml an <named-query> element outside of the <entity> element and it is xml-valid, so it is legal.
If you move the #NamedQuery in a class that is not an entity that won´t work as hibernate only scan the classes that are entities. You will find something like NamedQuery not found, also it is a good practice to have them in the entity that has more reference to them.
I am using Model First to create my database with Entity Framework 6.
I have a class as follows:
public partial class User : IdentityUser
{
}
Where User is a class generated by EF in the designer. However, how can I map the in inherited properties of User to the columns of a table?
My biggest issue is that despite the fact that I am adding the inheritance of IdentityUser to the User class/table, when I go back into the EF designer, the properties of IdentityUser do not show up.
If you are using a non-abstract base class and do not specify a [Table] on your User, and therefore your User and IdentityUser map to a single table, then you are using the TPH (Table Per Hierarchy: Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information) scenario.
So, it seems like this is the one you need. You don't have to do anything special in Code First to enable TPH. It's the default inheritance mapping strategy. Just map it as any other entity and you're done!
Mapping example:
I have 3 step inheritance
abstract Entity
abstract ApplicationEntity:Entity
abstract SystemEntity:ApplicationEntity
the system entity contains a many to many navigation property which when I try to load I get (202,10) : error 3034: Problem in mapping fragments starting at lines 202, 208:Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows.
(208,10) : error 3034: Problem in mapping fragments starting at lines 208, 578:Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows.
please help me
I'm going to post a solution here, but this may not fit your problem as this is for code first approach.
Solution was actually pretty simple. Just dress the base and derived classes with a unique table name, EF will represent each class as a separate table.
In practice (using VB but you get the point if C#):
the base class
Imports System.ComponentModel.DataAnnotations.Schema
Namespace Models
<Table("Notification")>
Public MustInherit Class NotificationBase
Public Property ID As Integer
...
End Class
End Namespace
a derived class
Imports System.ComponentModel.DataAnnotations.Schema
Namespace Models
<Table("EmergencyNotification")>
Public NotInheritable Class EmergencyNotifications
Inherits NotificationBase
...
End Class
End Namespace
I have the following questions
I have the '#Id' annotated field as part of my '#MappedSuperClass' and I am letting all my entities extend the MappedSuperClass.Now,how do I override the super class 'id' if I have to define a composite primary key for my entity...ie.How do I ask my entity to use its composite primary key annotated as #EmbeddedId instead of the #Id annotated field inherited from the MappedSuperClass? Will the #EmbeddedId annotation in my entity automatically over-ride the superclass's #Id ?
I have made a few fields (which are shared by most of the entities in my schema) as part of my MappedSuperClass. Now how do I avoid those fields getting added as columns if few of the entities don't need them ?
Thanks.
[...] How do I ask my entity to use its composite primary key annotated as #EmbeddedId instead of the #Id annotated field inherited from the MappedSuperClass? Will the #EmbeddedId annotation in my entity automatically over-ride the superclass's #Id ?
AFAIK, you can't. So don't inherit from your entity superclass in this case, use another entity superclass.
I have made a few fields (which are shared by most of the entities in my schema) as part of my MappedSuperClass. Now how do I avoid those fields getting added as columns if few of the entities don't need them ?
Well, again, don't inherit from the entity superclass that holds these fields and use another entity superclass.
JPA provides attribute-override to override the mappings for both embedded or mappedsuperclass fields.
For Annotations you can use the #AttributeOverride annotation within the java class.
For multiple attributes you can use the #AttributeOverrides annotation which contains an array of the #AttributeOverride annotation.