many-to-many

Fluent NHibernate Self Referencing Many To Many

情到浓时终转凉″ 提交于 2019-12-04 10:38:15
问题 I have an entity called Books that can have a list of more books called RelatedBooks. The abbreviated Book entity looks something likes this: public class Book { public virtual long Id { get; private set; } public virtual IList<Book> RelatedBooks { get; set; } } Here is what the mapping looks like for this relationship HasManyToMany(x => x.RelatedBooks) .ParentKeyColumn("BookId") .ChildKeyColumn("RelatedBookId") .Table("RelatedBooks") .Cascade.SaveUpdate(); Here is a sample of the data that

Creating many-to-many relationships on a nested rails form

痞子三分冷 提交于 2019-12-04 10:08:25
I am trying to create Groups, Users and Memberships (the many-to-many relationships) simultaneously. People can add users to the group as they create it and then I want it to route through to a view of the group with all the members. I can get the users to be created and the current user's membership to save to the database. However I'm struggling to get the id for the freshly created User objects in order to save the new memberships. Group.rb class Group < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships has_many :users accepts_nested_attributes_for :users,

How to cascade persist only new entities

試著忘記壹切 提交于 2019-12-04 09:26:28
I'm having trouble figuring out how to set up JPA persistence (using EclipseLink and transaction-type="RESOURCE_LOCAL") correctly for the following entities: @Entity public class User { // snip various members @ManyToMany private List<Company> companies; public void setCompanies(List<Company> companies) { this.companies = companies; } } @Entity public class Company { // snip various members } What I'm trying to do is set up a cascade for the companies list so that, if a new Company that hasn't been previously persisted is in the list, it would be automatically persisted together with the User:

Hibernate / JPA many to many relationship through a join table and a composite key, Unique Constraint issue

南楼画角 提交于 2019-12-04 08:18:00
So I asked this question yesterday, but the goal posts have changed and the question is different: Hibernate / JPA Collection of Elements with Many to Many relationship? I want to know if it's possible to create entities that will model my required relationship so that Hibernate will create my schema when I fire up my application. The relationship I want looks like this: 1 http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/417593_10150594114269218_505554217_8657377_1475865815_n.jpg The thing is that the Join table can actually contain rows that don't link to any Elements. The structure represents

Generation query when the ManyToMany relationship is used by Spring Data Jpa project

青春壹個敷衍的年華 提交于 2019-12-04 08:02:02
I've the following entities mapping: @Entity @Table(name = "books") public class Book implements Serializable { @ManyToMany @JoinTable(name="books2categories", joinColumns=@JoinColumn(name="book_id"), inverseJoinColumns=@JoinColumn(name="category_id")) Collection<Category> categories; ... @Entity @Table(name = "categories") public class Category implements Serializable { @ManyToMany(mappedBy="categories") private Collection<Book> books; BookRepository interface is looked: public interface BookRepository extends JpaRepository<Book, Long> { @Query("SELECT b FROM Book b INNER JOIN b.categories c

Adding values to “many to many ” relationship in entityframework

心不动则不痛 提交于 2019-12-04 07:13:36
问题 I have 3 entities in Sqlserver that I am mapping with the wizard of visual Studio The three entities are packages categories and packages_categories,where packages_categories an many to many relationship. After mapping These entities I am geting 2 classes wich are Categories and Packages,they look like this: public partial class Package { public Package() { this.Categories = new HashSet(); } public string PackageSid { get; set; } public string PackageName { get; set; } public virtual

MVC4 save data using Many to Many relationship

可紊 提交于 2019-12-04 07:03:54
问题 Many to Many Relationship protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Company>() .HasMany(c => c.Tags) .WithMany(t => t.Companies) .Map(m => { m.MapLeftKey("Companyid"); m.MapRightKey("tagid"); m.ToTable("CompanyTags"); } } Add company var company = new Company() { Name = "FooBar Inc" }; Add Tag int tagId = _db.Tags.Where(x => x.Title == tag).Select(x => x.Id).SingleOrDefault(); if (tagId==0) company.Add(new Tag { Title = tag}); else ?????? /

MappedBy in bi-directional @ManyToMany : what is the reason

淺唱寂寞╮ 提交于 2019-12-04 06:42:27
What is the reason for setting MappedBy in bidirectional many-to-many relationships? When one table has significant amount of records, while other has a few, which side is better to put mappedBy? It's actually a good question, and it helps to understand the concept of an "owning" entity. If you want to prevent both sides (in a bidirectional relationship) from having join tables , a good idea, then you need to have a mappedBy= element on one side. Whether or not there is a join table is controlled by the mappedBy="name" element of the @ManyToMany annotation. The Javadoc for mappedBy for the

Entity framework 4 many-to-many insertion?

♀尐吖头ヾ 提交于 2019-12-04 06:24:28
问题 I'm not very familiar with the many-to-many insertion process using Entity Framework 4, POCO. I have a blog with 3 tables: Post, Comment, and Tag. A Post can have many Tags and a Tag can be in many Posts . Here are the Post and Tag models: public class Tag { public int Id { get; set; } [Required] [StringLength(25, ErrorMessage = "Tag name can't exceed 25 characters.")] public string Name { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int Id {

Primary key violation on adding of relationship from many to many linked tables in MVC 3 entity framework

独自空忆成欢 提交于 2019-12-04 05:30:17
I've read through scores of questions on here that seem at first to have a similar problem but just don't seem quite the same. Massive apologies if this is answered somewhere but like I say I've read through loads and can't find an answer. I'm using Entity Framework and MVC 3. I'm trying to add tags to products in my entity framework, which have a many to many relationship and a table linking them with simply the two keys in the link table, so EF condenses the Tags into a property of Product. Tables are set up like so: Product: ProductID [int, primary key], Name, etc. Tags: TagName [string,