Home Answers Viewqa Hibernate jpa one-to-many persist children with its ID

 
 


Thadeuse
jpa one-to-many persist children with its ID
1 Answer(s)      7 months ago
Posted in : Hibernate

@Entity
public class Item {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column
 private Long id;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "item")
 private List<Feature> features= new ArrayList<Feature>();
........................................................................

public class Feature{
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 private String attribute1;
 private String attribute2;
 private String featureCode;

    @ManyToOne
 private Item item;

        .....
 public String getFeatureCode() {
  if (item != null) {
   feature = item.getId() + attribute1 + attribute2;
  }
  return feature;
 }

You can see from my annotaiton, When Item pesist, it will also persist its children.

As composited ID has a very bad reputation and I realy need its parent's ID in the Feature in order to easy query, I add an featureCode to append the three column together.

My problem is , the Item's id is generated from database. From the debug information, I guess it same Item first, then update Feature's item. This causes my featureCode's item.getId() null result.

Is it possible to change one-to-many's cascade persist behavoir, like persist Item first, then save it's children?

View Answers

October 10, 2012 at 5:20 PM


Please visit the following link:

JPA one to many Example









Related Pages:
jpa one-to-many persist children with its ID
jpa one-to-many persist children with its ID  I have two tables like Organization and region ,both table having one to many relationship.when i... persist the region table how? any one help me
jpa one-to-many persist children with its ID
jpa one-to-many persist children with its ID  @Entity public class... can see from my annotaiton, When Item pesist, it will also persist its children... to change one-to-many's cascade persist behavoir, like persist Item first, then save
jpa one-to-many persist children with its ID by EntityListeners
jpa one-to-many persist children with its ID by EntityListeners  jpa one-to-many persist children with its ID by EntityListeners
JPA One-to-Many Relationship
JPA One-to-Many Relationship     ... the one-to-many relationship and how to develop a one-to-many relation in your JPA... relationship: To develop the JPA One-to-Many relation, you need the following
Hibernate One-to-many Relationships
Hibernate One-to-many Relationships       Hibernate One-to-many Relationships - One to many example code in Hibernate using the xml file as metadata. Here
one to many video chatting in web application
one to many video chatting in web application  Hi all....I am developing a web application,where video chatting is its one of the main feature.Can anybody tell me which technology to be used.Currently i am using Jsp and servlet
JPA Examples
will read about how to map one-to-one relationship in JPA.   One-to-many... in JPA.   Many-to-one : In this section you will read about how to map many-to-one relationship in JPA.   Many-to-many
JPA Architecture
-based Java framework to persist the Java Objects to the relational database. JPA... about all these functions in next sections. We have developed many JPA examples... JPA Architecture      
JPA
JPA   I HAVE MADE A PROJECT IN I HAVE MADE ONE - ONE RELATION MY CODE... serialVersionUID = 1L; @Id @Basic(optional = false) @Column(name = "Stu_Id...) @Column(name = "Teacher_Id", nullable = false) private long teacherId
JPA Relationship
One-to-many: JPA defines a OneToMany mapping for a many-valued association... by TopLink JPA for uni-directional one-to-many relationships Many... of relationship: One-to-one One-to-many Many-to-one Many-to-many Many
Hibernate persist Method
Hibernate persist Method In this tutorial you will learn about how to use persist method in hibernate. Hibernate's persist() method is used to insert...() method but, it is different in the case that the save() method can return the id
Hibernate one-to-many relationships.
Hibernate one-to-many relationships.  How does one-to-many relationships works in hibernate
JPA Tutorial
entity beans programming model. Using JPA a Java developer can persist data...JPA Tutorial In this section we will discuss about the Java Persistence API. This section will describe you all the aspects of JPA such as what is JPA
JPA Examples in NetBeans
to develop a jpa application to find records according to its id... a jpa application to find records according to its name.  ... JPA Examples in NetBeans     
Resultset with one to many relationship
Resultset with one to many relationship   Suppose there are 5 tables in database named A,B,C,D and E. A has one to many relationship with B,C,D and D has one to many relationship with E. Beam class of A contains array of Class B
JPA setParameter (Named Parameter)
JPA setParameter (Named Parameter)       In this section, you will learn the JPA Query API's method setParameter as a named parameter. JPA Query API's supports two
JPA Many-to-one Relationship
JPA Many-to-one Relationship   ... in your JPA Application. Many-to-One: In this relationship many related records... many-to-one relationship: To develop the JPA Many-to-One relation, you need
JPA Named Query
JPA Named Query       In this section, you will read about the JPA Query, its types... by many classes and instances. For developing JPA Named Query, you need
JPA Training
JPA Training     The Java... specification. JPA Stands for Java Persistence API, also known as EJB3 Persistence... and the Java collections framework.  JPA Training Objectives  
JPA Introduction
and persist the Java Objects. ORM frameworks that can be used with JPA are Hibernate... of any size Application developed in JPA is portable across many servers... versions of JPA will provide many features. In the next session we will learn
Result Set one to many mapping
Result Set one to many mapping  Suppose there are 5 tables in database named A,B,C,D and E. A has one to many relationship with B,C,D and D has one to many relationship with E. Beam class of A contains array of Class B,C, and D
JPA Native Queries, JPA Native Queries Tutorials
JPA Native Queries       In this section, you will know about the jpa native queries and how to use native query in your JPA application.  JPA Native Queries: JPA native
JPA Examples In Eclipse
Subquery and many more. So, let's get started with JPA Examples using Eclipse IDE...; JPA Named Query In this section, you read about the JPA Query, its types... the JPA Query, its types and how to use the JPA Named Query in your JPA
id creation
id creation  CREATE TABLE mohan2 ( sno bigint(20) NOT NULL AUTO_INCREMENT, name varchar(30) NOT NULL, sal int(30) NOT NULL, eid varchar..."); } } how can write this program in jsp its working on console
Hibernet one-to-many prjection accessing - Hibernate
Hibernet one-to-many prjection accessing  Hai, I am Vinayak. I have Doubt in hibernate one-to-many accessing projection. Explanation of Question. I have a table PERSONAL and ADDRESS. PERSONAL is one to many association
Hibernate 4 One to Many mapping using XML
In this section, you will learn how to do one to many mapping of tables in Hibernate using Xml
JPA Ordering
JPA Ordering       In this section, you will learn about the JPA Ordering and how to develop JPA application using ordering. JPQL queries can contains ORDER BY clause
JPA Grouping
JPA Grouping       In this section, you will learn about the JPA Grouping and how to use it in your JPA application. The GROUP BY clause is used with SELECT statement to retrieve
Hibernate One to Many Indexed Mapping
In this section, you will learn to one to many indexed mapping in Hibernate to preserve mapping order
JPA
JPA  HOW CAN I WRITE A SIMPLE PROGRAM IN NETBEANS CONSOLE ON JPA ON CRUD APPLICATION? HOW CAN I START KINDLY HELP ME
Hibernate One to Many Self Join using Annotations
In this section, you will learn one to many self join using Annotations in Hibernate
JPA executeUpdate
JPA executeUpdate      ... in your  JPA application. To use executeUpdate() method in your jpa application for executing a jpa UPDATE/DELETE query. This method returns the number of objects
JPA getSingleResult
JPA getSingleResult      ... is your JPA application.  getSingleResult(): This is the method... `student` (  `id` int(11) NOT NULL auto_increment, 
JPA
JPA  Sir, I have tried making the jpa application which you have given on this site . But then also I m facing problem in that like : If i make then also it is giving error. If I try to run the application provided by you
Database Record findById
;    This section, you will see how to develop a jpa application to find records according to its id( primary key). EntityManager interface... Table: student CREATE TABLE `student` (  `id` int(11
JPA update data Example
JPA update data Example       In this section, you know how to update the database data through the jpa... the business object Update the data Save (persist) the data using
Hibernate One to many XML mapping array Example
In this section, you will learn how to do one to many mapping using array to retain mapping order in Hibernate
Hibernate One to Many XML Mapping list Example
In this section, you will learn how to do one to many mapping using List to retain mapping order in Hibernate
Hibernate One to Many Bi-directional Mapping
In this section, you will learn how to do One to Many Bi-Directional Mapping in Hibernate
Hibernate 4 One to Many mapping using Annotation
In this section, you will learn how to do one to many mapping in Hibernate using Annotation
Hibernate 4 One to Many mapping using Annotation
In this section, you will learn how to do one to many mapping in Hibernate using Annotation
JPA-QL Queries
JPA-QL Queries       In this section, we will use the simple JPA-QL query in our JPA application. Here JPA-QL query used with createQuery() method of EntityManager
JPA Named Parameter
JPA Named Parameter       In this section, you will learn how to use JPA Named Parameter in your jpa application. In JPA named parameter is used where you assign names
JPA Positional Parameter
JPA Positional Parameter       In this section, you will learn about the JPA Positional Parameter and how to use in your JPA application. JPA Queries are convenient to pass
JPA getResultList Example
JPA getResultList Example   ... the getResultList() method in your JPA application.  getResultList(): This method... `student` (  `id` int(11) NOT NULL auto_increment, 
JPA Named Queries
JPA Named Queries       In this section, you will know about the JPA Named Queries and how to use in JPA application. @NamedQueries: If you need to use more than one
JPA Named Parameter List
JPA Named Parameter List       In this section, you will learn how to develop a JPA application using JPA Named Parameter List. This is similar as named parameter
Join problem with JPA Criteria Builder
Join problem with JPA Criteria Builder  I need to make this Query...); And the query I get is the next one: select setting0_.code..._, settingval1_.setting_value as col_5_0_, user2_.name as col_6_0_, user2_.id
JPA Count Function
JPA Count Function       In this section, you will learn about the count function of JPA aggregate function. JPA Count function counts all records to database table. JPA Count
JPA Tutorials
? The Java Persistence API or JPA for sort is one to the specification of JEE5... JPA Tutorials          JPA Tutorials and Example: Here we