one-to-many

Hibernate @Version annotation and object references an unsaved transient instance

十年热恋 提交于 2019-11-27 19:40:07
问题 My New Project is in Hibernate 4.2.5.Final and Spring. After Login, I am storing the user object in the session. Now after successful login, I need to insert one record in the application log. Here are the classes: Class BaseEntity @MappedSuperclass public abstract class BaseEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long ID; @Version private Long version; private Long createdBy; @Temporal

AttributeError: 'int' object has no attribute '_sa_instance_state'

安稳与你 提交于 2019-11-27 19:36:31
I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User. models.py class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), index=True, unique=True) password = db.Column(db.String(32), index=True) email = db.Column(db.String(120), index=True, unique=True) role = db.Column(db.SmallInteger, default=ROLE_USER) posts =

Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First

∥☆過路亽.° 提交于 2019-11-27 19:25:57
问题 i tried the whole day to get this working. I learned a lot about EF's Fluent API (e.g. this is an excellent article), however i had no success. I have three Entities: public class Address { [Key] public virtual int AddressId { get; set; } public virtual string AddressString { get; set; } } public class User { [Key] public virtual int UserId { get; set; } public virtual ICollection<Address> Addresses { get; set; } } public class House { [Key] public virtual int HouseId { get; set; } public

Hibernate/JPA ManyToOne vs OneToMany

你离开我真会死。 提交于 2019-11-27 10:44:34
I am reading currently the documentation of Hibernate regarding the entity associations and I come accross a little difficulty to figure out some things. It has to do in essence with the difference between ManyToOne and OneToMany associations. Although I have used them in real projects, I cannot apprehend completely the differnce between them. To my understanding, if a table / an entity has a ManyToOne association with another, then the association should be from the other side OneToMany . So, how should we decide which one to choose based on a specific case and how does it affect the database

One-To-Many relationship gets duplicate objects without using “distinct”. Why?

自作多情 提交于 2019-11-27 10:44:16
I have 2 classes in a one-to-many relationship and a HQL query that is a bit strange. Even if I have read some questions already posted, it does not seem clear to me. Class Department{ @OneToMany(fetch=FetchType.EAGER, mappedBy="department") Set<Employee> employees; } Class Employee{ @ManyToOne @JoinColumn(name="id_department") Department department; } When I use the following query I get duplicates Department objects: session.createQuery("select dep from Department as dep left join dep.employees"); Thus, I have to use distinct: session.createQuery("select distinct dep from Department as dep

Hibernate inserts duplicates into a @OneToMany collection

随声附和 提交于 2019-11-27 09:02:52
I have a question concerning Hibernate 3.6.7 and JPA 2.0. Consider following entities (some getters and setters are omitted for brevity): @Entity public class Parent { @Id @GeneratedValue private int id; @OneToMany(mappedBy="parent") private List<Child> children = new LinkedList<Child>(); @Override public boolean equals(Object obj) { return id == ((Parent)obj).id; } @Override public int hashCode() { return id; } } @Entity public class Child { @Id @GeneratedValue private int id; @ManyToOne private Parent parent; public void setParent(Parent parent) { this.parent = parent; } @Override public

NHibernate configuration for uni-directional one-to-many relation

ε祈祈猫儿з 提交于 2019-11-27 07:39:24
I'm trying to set up a relationship as follows. Each Master item has one or more Detail items: public class Detail { public virtual Guid DetailId { get; set; } public virtual string Name { get; set; } } public class Master { public virtual Guid MasterId { get; set; } public virtual string Name { get; set; } public virtual IList<Detail> Details { get; set; } } And Mappings: public class MasterMap : ClassMap<Master> { public MasterMap() { Id(x => x.MasterId); Map(x => x.Name); HasMany(x => x.Details).Not.KeyNullable.Cascade.All(); } } public class DetailMap : ClassMap<Detail> { public DetailMap(

How to configure a One-to-Many relationship in EF

假装没事ソ 提交于 2019-11-27 07:38:56
问题 I have the following model public class PageConfig : Base { // Properties Etc.. public ICollection<Image> ScrollerImages { get; set; } } My approach is to bind using a junction table { PageConfigID, ImageID }. In my model binder i tried the following.. modelBuilder.Entity<PageConfig>() .HasMany(x => x.ScrollerImages) .WithMany() .Map(x => { x.ToTable("junc_PageConfigScrollerImages"); x.MapLeftKey("PageConfigID"); x.MapRightKey("ImageID"); }); Which results in a null collection of images. How

@OneToMany and composite primary keys?

点点圈 提交于 2019-11-27 07:06:03
I'm using Hibernate with annotations (in spring), and I have an object which has an ordered, many-to-one relationship which a child object which has a composite primary key, one component of which is a foreign key back to the id of the parent object. The structure looks something like this: +=============+ +================+ | ParentObj | | ObjectChild | +-------------+ 1 0..* +----------------+ | id (pk) |-----------------| parentId | | ... | | name | +=============+ | pos | | ... | +================+ I've tried a variety of combinations of annotations, none of which seem to work. This is the

Doctrine 2 OneToMany Cascade SET NULL

梦想与她 提交于 2019-11-27 07:02:57
The error Cannot delete or update a parent row: a foreign key constraint fails. The classes class Teacher { /** *@ORM\OneToMany(targetEntity="publication", mappedBy="teacher") */ protected $publications; } class Publication { /** * @ORM\ManyToOne(targetEntity="Teacher", inversedBy="publications") * @ORM\JoinColumn(name="teacher_id", referencedColumnName="id") */ protected $teacher; } I want What I want is to make it that when you delete a teacher, the id_teacher is modified to NULL. I want to keep the publication but without reference to Professor. I don't know how do that in Doctrine, Is it