Data annotations composite key9/26/2023 The instructors gave us a WSDL, a database and some instructions. On the AMIS SOA training program we got a nice assignment. In this article I will explain how to map a composite primary key with JPA-annotations and how to work around the bug in Hibernate Annotations. When you’re using Hibernate Annotations you might also run into an annoying bug. You could then also use data annotations ( attribute) instead of Fluent API.A table without single-column primary key, Java developers don’t like them because it’s more work than just putting on a field. It would only be required if you had unconventional names (like PostFK or something). Because the naming of your foreign key properties follows the naming convention EF uses for mapping you can omit the Fluent mapping altogether. The foreign key properties must be nullable ( int?) for this as you already found. To get a one-to-many relationship you can specify the following mapping in Fluent API: modelBuilder.Entity() You have to decide then which kind of inheritance mapping you want to use - TPH, TPT or TPC. This would select all posts of a particular user but not the polls. You don't need the TypeOfPost then anymore because you can filter the two concrete types using the OfType LINQ operator, for example: var x = () Then the model would look like this: public abstract class BasePost // your former Posts class For a one-to-one relationship you also have to remove the foreign key columns PostId and PollId because Posts refers through its primary key to the Post and Poll.Īn alternative approach which seems to be appropriate in your model is inheritance mapping. Only in a one-to-many relationship you would have a separate column for the foreign key. The principal has the primary key in the relationship, the dependent the foreign key which is also the primary key at the same time in Post/ Poll table because it is a one-to-one relationship. This makes Posts automatically the principal in the relationship and Post or Poll the dependent. I would probably try to create the two one-to-one relationships as optional:required because a Poll must have a reference to Posts and a Post also must have a reference to Posts: modelBuilder.Entity() In addition, what is the actual difference between WithOptionalDependent and WithOptionalPrincipal? - I have read on MSDN, but, I really do not understand the difference. If I am correct here, Should the go on the virtual property, or the ID of the property? I believe the first one is the one I need, but, I need to use it in combination with in the Post Class. The first one seems to create an additional column in the database that allows nulls, and the second one doesn't appear to do anything. HasOptional(x => x.Poll).WithOptionalPrincipal() HasOptional(x => x.Poll).WithOptionalDependent() I have tried both of the following: modelBuilder.Entity() (If anyone knows a better way, please say!).Īnyway, the problem is, I can get this working fine by going in to SQL Management Studio and manually editing the schema to allow nulls - but, I just can't work out how to do this in the FluentAPI, and need some help. I don't think there is anyway of setting "Only one required" or similar, so, I handle all the checking and stuff in the application. I have a (sample) application with the following code: public class Postsīasically, I don't know if there is a better way of doing this, but, I have a list of Posts, and, people can choose if it is a Poll or a Post, As Entity Framework doesn't work with Enums, I just store it as a string in TypeOfPost and then in the application, I programmatically query either Poll or Post based on the value of TypeOfPost.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |