many-to-one

Hibernate ManyToOne vs OneToOne

两盒软妹~` 提交于 2019-12-17 23:05:11
问题 I can't see any difference in the schema of a Many-To-One relationship vs a OneToOne relationship: @Entity public class Order { @ManyToOne @JoinColumn(nullable = false) private Address address; vs @Entity public class Order { @OneToOne @JoinColumn(nullable = false) private Address address; Is there any difference? 回答1: They look exactly the same on schema but there is difference on Hibernate Layer. If you try something like that: Address address = new Address(); Order order1 = new Order();

How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?

巧了我就是萌 提交于 2019-12-17 19:22:06
问题 I'm asking and answering my own question, but i'm not assuming i have the best answer. If you have a better one, please post it! Related questions: How to set a backreference from an @EmbeddedId in JPA hibernate mapping where embeddedid (?) JPA Compound key with @EmbeddedId I have a pair of classes which are in a simple aggregation relationship: any instance of one owns some number of instances of the other. The owning class has some sort of primary key of its own, and the owned class has a

Django change foreign key data and save

心不动则不痛 提交于 2019-12-13 07:12:49
问题 I have two models like class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE) Now for an object of Article lets say a=Article.objects.filter(id=1) a=a[0] I try to change the headline and the email of the author who has written this

Many to one configuration using EF 4.1 code first

北战南征 提交于 2019-12-13 07:04:12
问题 I have the following classes: public class CartItem { public long Id { get; set; } public int Quantity { get; set; } public Product Product { get; set; } } public class Product { public long Id { get; set; } public string Title { get; set; } public decimal Price { get; set; } } I currently have the following configuration: modelBuilder.Entity<CartItem>().HasRequired(x => x.Product).WithMany().Map(x => x.MapKey("ProductId")); I am trying to ensure that whenever I retrieve a cartitem from the

JPA - OneToMany relationship not loading children

拟墨画扇 提交于 2019-12-13 06:26:04
问题 I am trying to configure this @OneToMany and @ManyToOne relationship but it's simply not working, not sure why. I have done this before on other projects but somehow it's not working with my current configuration, here's the code: public class Parent { @OneToMany(mappedBy = "ex", fetch= FetchType.LAZY, cascade=CascadeType.ALL) private List<Child> myChilds; public List<Child> getMyChilds() { return myChilds; } } public class Child { @Id @ManyToOne(fetch=FetchType.LAZY) private Parent ex; @Id

Cross fetch created on bi-directional One-to-many JPA / Hibernate query

拜拜、爱过 提交于 2019-12-13 03:58:17
问题 Here's the basic mapping: Client { @OneToMany(mappedBy="client",cascade=CascadeType.ALL, fetch=FetchType.EAGER) private Set<Group> groups = new HashSet<Group>(); } Group { @ManyToOne (cascade=CascadeType.ALL) private Client client = new Client(); } The problem I'm having is that when I query against Client, I'm getting a full client for each associated group. My queries are pretty simple and I've tried both criteria and HQL. Here's a sample criteria query: Criteria crit = getSession()

ASP.NET MVC 5 - Scaffolding for Many to One Relationship

你离开我真会死。 提交于 2019-12-13 02:23:39
问题 I am Working on ASP.NET MVC 5, EF 6, Razor Engine, VB Language and Database First Approach with VS 2013. Now, in my DB; I have two tables as below: CREATE TABLE [dbo].[Group] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY(1, 1), [Name] VARCHAR(50) NOT NULL ) and CREATE TABLE [dbo].[Subscriber] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY(1, 1), [FirstName] [nvarchar](100) NOT NULL, [MiddleName] [nvarchar](100) NULL, [LastName] [nvarchar](100) NOT NULL, [Email] [varchar] (200) NOT NULL UNIQUE, [GroupId]

NHibernate many-to-one mapping: If parent is null, set foreign-key as empty Guid instead of null

谁都会走 提交于 2019-12-12 16:59:50
问题 What I am trying to is really quite straigh forward, but I cannot seem to get the mapping right with NHibernate. I am working up against a database with parent and child objects. The child objects have a foreign key reference to the primary key of the parent which is of datatype Guid. Pretty normal in any case. Now the database is set up in such a way that the foreign key field must never be null, so in case of orphan objects, with no parent, the foreign key should be an empty Guid ('00000000

Why does Hibernate attempt to load “not-found=ignore” associations?

安稳与你 提交于 2019-12-12 12:35:41
问题 I've got a class (representing a project) which maps another class (representing a user) through a many-to-one relationship. As users come and go I've made sure to set the not-found property to ignore on the relationship. However, it appears that Hibernate still attempts to load the "missing" users by executing one extra SQL query for each of the projects where the not-found property should have set the relationship to null . That is, in cases where the associated user no long exist, I would

Multiple relationships with single mapping table without generating foreign keys by Hibernate

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:18:02
问题 I have two base abstract classes and there are multiple additional classes derived from these two, adding additional attributes etc. There exist relations between those specific derived types. A simple illustrating example: Role and Group classes are abstract , but they are not marked as @MappedSuperclass . InheritanceType.JOINED strategy is being used, so both tables Role (for abstract class) and AdminRole (for derived class) should exist (they both will have the same RoleID ).