jpa one-to-many persist children with its ID

jpa one-to-many persist children with its ID

@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 Tutorials/Questions & Answers:
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
Advertisements
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... one-to-many relationship:ADS_TO_REPLACE_2 To develop the JPA One-to-Many
Hibernate one-to-many relationships.
Hibernate one-to-many relationships.  How does one-to-many relationships works in hibernate
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
Hibernate One-to-many Relationships
=? where id=?   In this section we learned about the one-to-many... Hibernate One-to-many Relationships       Hibernate One-to-many Relationships
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
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
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
Hibernate One to Many Indexed Mapping
In this section, you will learn to one to many indexed mapping in Hibernate to preserve mapping order
Hibernate One to Many Self Join using Annotations
In this section, you will learn one to many self join using Annotations 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
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 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 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
Maven Repository/Dependency: com.github.honoluluhenk.id-class | id-class-jpa-hibernate-integration
Maven Repository/Dependency of Group ID com.github.honoluluhenk.id-class and Artifact ID id-class-jpa-hibernate-integration. Latest version of com.github.honoluluhenk.id-class:id-class-jpa-hibernate-integration dependencies
Maven Repository/Dependency: com.persist | persist-logging_2.12
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-logging_2.12. Latest version of com.persist:persist-logging_2.12 dependencies. # Version Release Date You can
Maven Repository/Dependency: com.persist | persist-json_2.9.2
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-json_2.9.2. Latest version of com.persist:persist-json_2.9.2 dependencies. # Version Release Date You can read
Maven Repository/Dependency: com.persist | persist-json_2.12
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-json_2.12. Latest version of com.persist:persist-json_2.12 dependencies. # Version Release Date 1
Maven Repository/Dependency: com.persist | persist-json_2.11
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-json_2.11. Latest version of com.persist:persist-json_2.11 dependencies. # Version Release Date 1
Maven Repository/Dependency: com.persist | persist-json_2.10
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-json_2.10. Latest version of com.persist:persist-json_2.10 dependencies. # Version Release Date 1
Maven Repository/Dependency: com.persist | persist-logging_2.11
Maven Repository/Dependency of Group ID com.persist and Artifact ID persist-logging_2.11. Latest version of com.persist:persist-logging_2.11 dependencies. # Version Release Date 1
Children's Writing
Article Writing for Children Future lies in hands of Children or a child... on what should be the content while writing articles for children. Do we understand.... In this environment writing articles for children is more of a task of responsibility
Maven dependency for com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration version 1.0.1 is released. Learn to use id-class-jpa-hibernate-integration version 1.0.1 in Maven based Java projects
1.0.1 of id-class-jpa-hibernate-integration released The developers of   com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration project... version of  com.github.honoluluhenk.id-class - id-class-jpa-hibernate
Maven dependency for com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration version 1.0.0 is released. Learn to use id-class-jpa-hibernate-integration version 1.0.0 in Maven based Java projects
1.0.0 of id-class-jpa-hibernate-integration released The developers of   com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration project... version of  com.github.honoluluhenk.id-class - id-class-jpa-hibernate
Maven dependency for com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration version 0.0.4 is released. Learn to use id-class-jpa-hibernate-integration version 0.0.4 in Maven based Java projects
0.0.4 of id-class-jpa-hibernate-integration released The developers of   com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration project... version of  com.github.honoluluhenk.id-class - id-class-jpa-hibernate
Maven dependency for com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration version 0.0.3 is released. Learn to use id-class-jpa-hibernate-integration version 0.0.3 in Maven based Java projects
0.0.3 of id-class-jpa-hibernate-integration released The developers of   com.github.honoluluhenk.id-class - id-class-jpa-hibernate-integration project... version of  com.github.honoluluhenk.id-class - id-class-jpa-hibernate
JPA Relationship
removed One-to-many: JPA defines a OneToMany mapping for a many... of the join table used by TopLink JPA for uni-directional one-to-many relationships Many-to-one: JPA defines a ManyToOne mapping for a single-valued
JPA Examples
relationship in JPA.   Many-to-one : In this section you will read about how to map many-to-one relationship in JPA.   Many... and its types as well as how to use JPA named query in your application
JPA Architecture
-based Java framework to persist the Java Objects to the relational database. JPA... these functions in next sections. We have developed many JPA examples to help you... JPA Architecture      
JPA
JPA  HOW CAN I WRITE A SIMPLE PROGRAM IN NETBEANS CONSOLE ON JPA ON CRUD APPLICATION? HOW CAN I START KINDLY HELP ME
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
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
Maven Repository/Dependency: com.plugback | jpa
Maven Repository/Dependency of Group ID com.plugback and Artifact ID jpa. Latest version of com.plugback:jpa dependencies. # Version Release Date You can read more at: Maven
JPA Framework
JPA Framework  Hi, Can someone tell me about the JPA Framework and its Examples in Eclipse. Please give me details about. Thanks.   Hi, you can get details about the JPA Framework with suitable examples on the below
id
id  how to find date of birth and gender in id in html with the help of javascript
JPA Tutorial
persist data for the desktop application as well as web application. JPA uses... the life cycle of entity instances. Persist the entity instance. JPA Querying...JPA Tutorial In this section we will discuss about the Java Persistence API
Entities in JPA 2.1
to persist an entity. Check the tutorial CRUD application in JPA 2.1... types of relationships: One-to-one One-to-many Many-to-one Many-to-many Read more JPA 2.1 tutorials.ADS_TO_REPLACE_6
ModuleNotFoundError: No module named 'persist'
ModuleNotFoundError: No module named 'persist'  Hi, My Python... 'persist' How to remove the ModuleNotFoundError: No module named 'persist... to install padas library. You can install persist python with following command
Maven Repository/Dependency: com.agapsys | jpa-utils
Maven Repository/Dependency of Group ID com.agapsys and Artifact ID jpa-utils. Latest version of com.agapsys:jpa-utils dependencies. # Version Release Date 1 0.1.0-20170527
Maven Repository/Dependency: com.gtcgroup | justify-jpa
Maven Repository/Dependency of Group ID com.gtcgroup and Artifact ID justify-jpa. Latest version of com.gtcgroup:justify-jpa dependencies. # Version Release Date 1
Maven Repository/Dependency: com.edugility | jpa-converters
Maven Repository/Dependency of Group ID com.edugility and Artifact ID jpa-converters. Latest version of com.edugility:jpa-converters dependencies. # Version Release Date You can read more
Maven Repository/Dependency: com.intersult | jpa-support
Maven Repository/Dependency of Group ID com.intersult and Artifact ID jpa-support. Latest version of com.intersult:jpa-support dependencies. # Version Release Date You can read more
Maven Repository/Dependency: com.intersult | jpa-fix
Maven Repository/Dependency of Group ID com.intersult and Artifact ID jpa-fix. Latest version of com.intersult:jpa-fix dependencies. # Version Release Date You can read more at: Maven
Maven Repository/Dependency: org.swarmic | jpa-hibernate
Maven Repository/Dependency of Group ID org.swarmic and Artifact ID jpa-hibernate. Latest version of org.swarmic:jpa-hibernate dependencies. # Version Release Date You can read more
Maven Repository/Dependency: org.wildfly.swarm | jpa-mysql
Maven Repository/Dependency of Group ID org.wildfly.swarm and Artifact ID jpa-mysql. Latest version of org.wildfly.swarm:jpa-mysql dependencies. # Version Release Date 1
Maven Repository/Dependency: org.datanucleus | datanucleus-jpa
Maven Repository/Dependency of Group ID org.datanucleus and Artifact ID datanucleus-jpa. Latest version of org.datanucleus:datanucleus-jpa dependencies. # Version Release Date 1

Ads