-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: 'Insert... Select' Statement with Embedded Object
PostPosted: Tue Jun 12, 2012 8:56 am 
Newbie

Joined: Tue Jun 12, 2012 8:45 am
Posts: 3
I have a persistable object (we shall call it ObjectA) with an embedded object (ObjectB) as a field (objectB). If I create a new ObjectA, define and set objectB (and any other parameters) and persist it, then everything works fine.

Note: From the database point of view, ObjectB's fields are stored in the same table as ObjectA's fields.

Unfortunately I need to do an 'Insert... Select' statement, to transfer data from one table to another and I must use HQL (SQL is not an option). I would be grateful if anyone has a solution.

Just to let you know, I have tried doing things like...

insert into ObjectA (id, param1, objectB.paramb1, objectB.paramb2)
SELECT new_id, new_param1, 'some text', 'some more text'
FROM SomeOtherObject

This did not work.

If you need more information then please let me know.


Top
 Profile  
 
 Post subject: Re: 'Insert... Select' Statement with Embedded Object
PostPosted: Wed Jun 13, 2012 6:14 am 
Newbie

Joined: Tue Jun 12, 2012 8:45 am
Posts: 3
OK in an effort to make the question more easily answered I will attach code for the 3 classes mentioned and also an updated version of the statement that did not work at the end of my last post.

ObjectA.java
Code:
import javax.persistence.Basic;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
@Table(name = "OBJECT_A")
public class ObjectA {

   private Long id;
   private String param1;
   private ObjectB objectB;

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
   @Column(name = "OBJECT_A_ID")
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }

   @Basic
   public String getParam1() {
      return parama1;
   }
   public void setParam1(String param1) {
      this.param1 = param1;
   }

   @Embedded
   public ObjectB getObjectB() {
      return objectB;
   }

   public void setObjectB(ObjectB objectB) {
      this.objectB = objectB;
   }
}


ObjectB.java
Code:
import javax.persistence.Basic;
import javax.persistence.Embeddable;

@Embeddable
public class ObjectB {

   private String paramb1;
   private String paramb2;

   @Basic
   public String getParamb1() {
      return paramb1;
   }
   public void setParamb1(String paramb1) {
      this.paramb1 = paramb1;
   }

   @Basic
   public String getParamb2() {
      return paramb2;
   }
   public void setParamb2(String paramb2) {
      this.paramb2 = paramb2;
   }
}


SomeOtherObject.java
Code:
import javax.persistence.Basic;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
@Table(name = "OTHER_OBJECT")
public class SomeOtherObject {

   private Long id;
   private String newParam1;
   private Long newId;

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
   @Column(name = "OTHER_OBJECT_ID")
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }

   @Basic
   public Long getNewId() {
      return newId;
   }
   public void setNewId(Long newId) {
      this.newId = newId;
   }

   @Basic
   public String getNewParam1() {
      return newParam1;
   }
   public void setNewParam1(String newParam1) {
      this.newParam1 = newParam1;
   }
}



I broke the typical naming conventions in the example I gave in the first post, so I have updated it as follows...

insert into ObjectA (id, param1, objectB.paramb1, objectB.paramb2)
SELECT newId, newParam1, 'some text', 'some more text'
FROM SomeOtherObject

This does not work, I could do with a solution that does. I have looked around and cannot find any reference to inserting into embedded objects with an Insert Select statement.

BTW in an effort to keep the source code protected, I have had to simplify and refactor. If you notice anything wrong then please let me know.


Top
 Profile  
 
 Post subject: Re: 'Insert... Select' Statement with Embedded Object
PostPosted: Mon Jun 18, 2012 8:11 am 
Newbie

Joined: Tue Jun 12, 2012 8:45 am
Posts: 3
The only solution that I have found to this problem is to introduce another object that you can retrieve an instance of ObjectB from. Since I needed all of my new ObjectA instances to have identical values in their objectB, this got round the issue entirely. So the query looked approximately like...

insert into ObjectA (id, param1, objectB)
SELECT s.newId, s.newParam1, y.objectB
FROM SomeOtherObject s, YetAnotherObject y
WHERE y.id = 1

Again, refactoring/simplifying has been done so apologies if I've made any mistakes. If anyone has a better (less hacky) way to implement this so that individual parameters can be set then I would be interested, and I would request a redistribution of points.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.