many-to-one

Construct JPA query for a OneToMany relation

╄→гoц情女王★ 提交于 2019-11-28 10:14:28
I've those 2 entities Class A { @OneToMany(mappedBy="a") private List<B> bs; } Class B { @ManyToOne private A a; private String name; } 1) I would like to construct a query that says get all A's that have at least one B with name ="mohamede1945" 2) I would like to construct a query that says get all A's that don't have any B with name = "mohamede1945" Could anyone help me? You can use the ANY and ALL constructs to filter the subquery. So something like 1. FROM A aEntity WHERE 'mohamede1945' = ANY (SELECT bEntity.name FROM aEntity.bs bEntity) 2. FROM A aEntity WHERE 'mohamede1945' <> ALL

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

旧街凉风 提交于 2019-11-28 10:13:34
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 many-to-one to this class via a corresponding foreign key. I would like the owned class to have a

Hibernate Many to one updating foreign key to null

淺唱寂寞╮ 提交于 2019-11-28 09:32:55
I am trying to get my @OneToMany and @ManyToOne relationships correct. Class 1: @Entity public class IdeaProfile { @Id @GeneratedValue private int ideaProfileId; private String name; Date dateConcieved; @OneToOne @JoinColumn(name="statusCode") private Status status; @OneToMany(fetch=FetchType.EAGER, targetEntity=Pitch.class, cascade=CascadeType.ALL) @JoinColumn(name = "ideaProfileId") private List<Pitch> pitchs; ....getters and setters.... Class2: @Entity public class Pitch { @Id @GeneratedValue private int id; @ManyToOne @JoinColumn(name = "ideaProfileId") private IdeaProfile ideaProfile;

Cannot make @ManyToOne relationship nullable

老子叫甜甜 提交于 2019-11-27 20:10:46
I have a many-to-one relationship that I want to be nullable: @ManyToOne(optional = true) @JoinColumn(name = "customer_id", nullable = true) private Customer customer; Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can anyone explain this? Is there a way to make it work? Note that I use JBoss 7, JPA 2.0 with Hibernate as persistence provider and a PostgreSQL 9.1 database. EDIT : I found the cause of my problem. Apparently it is due to the way I defined the primary key in the referenced entity Customer : @Entity @Table public class Customer { @Id @GeneratedValue @Column

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

Hibernate @ManyToOne only works with CascadeType.ALL

≡放荡痞女 提交于 2019-11-27 18:05:24
问题 I am using Hibernate 3.3.1 and i would like to create a relation between persons and an assigned company. They should be loosely coupled, but i would like to arrange to create a company via cascade and not explicitly calling saveOrUpdate(newCompany). I defined the following entities: class Company { @Id Long companyId; String name; } class Person { @Id Long personId; String name; @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}) Company company; } inside my

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

IllegalStateException with Hibernate 4 and ManyToOne cascading

核能气质少年 提交于 2019-11-27 08:51:18
I've got those two classes MyItem Object: @Entity public class MyItem implements Serializable { @Id private Integer id; @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) private Component defaultComponent; @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) private Component masterComponent; //default constructor, getter, setter, equals and hashCode } Component Object: @Entity public class Component implements Serializable { @Id private String name; //again, default constructor, getter, setter, equals and hashCode } And I'm tring to persist those with the following code

JPA delete all entites works strange

余生长醉 提交于 2019-11-27 08:30:44
问题 I have an entityrelation like this: In the ParentObj class: @OneToMany(mappedBy = "parentObj", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private List<ChildObj> list; In the ChildObj class: @JoinColumn(name="PARENT_OBJ") @ManyToOne private ParentObj parentObj; When the parent object persisted or removed, the child is persisted/removed as well. BUT when I try to remove all the entities with a CriteriaDelete like: CriteriaDelete<ParentObj> query = builder

Construct JPA query for a OneToMany relation

一笑奈何 提交于 2019-11-27 03:31:25
问题 I've those 2 entities Class A { @OneToMany(mappedBy="a") private List<B> bs; } Class B { @ManyToOne private A a; private String name; } 1) I would like to construct a query that says get all A's that have at least one B with name ="mohamede1945" 2) I would like to construct a query that says get all A's that don't have any B with name = "mohamede1945" Could anyone help me? 回答1: You can use the ANY and ALL constructs to filter the subquery. So something like 1. FROM A aEntity WHERE