Closed sbheda closed 11 years ago
Please share your entity definition and CRUD code.
Also, before that, please try getting your code running on version 2.4.
Sincerely, Amresh
On Mon, Apr 8, 2013 at 1:40 PM, shraddha notifications@github.com wrote:
Hi,
I am using Kundera 2.3.1. I have tried to add data into mysql db which has one to one relationship but i am not able to save it..
It throws org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing exception...
PN: We are trying to add auto incremental id.
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SpringExampleDAO springExampleDAO = (SpringExampleDAO)context.getBean("springExampleDAO");
Address address = new Address("Shiv park society"); Person person = new Person("Shraddha", 25); person.setAddress(address); EntityManager entityManager = springExampleDAO.getDataAccessLayer().getEntityManagerFactory().createEntityManager(); entityManager.getTransaction().begin(); entityManager.persist(person); entityManager.getTransaction().commit(); entityManager.close();
— Reply to this email directly or view it on GitHubhttps://github.com/impetus-opensource/Kundera/issues/236 .
Thanks Amresh for your quick reply,
We have tried into version 2.4 but it's not working
plz find below persistence.xml file
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
Java Code
Address address = new Address();
address.setStreet("Shiv park society");
Person person = new Person();
person.setPersonName("ABC");
person.setAge(25);
person.setAddress(address);
EntityManager entityManager = Persistence.createEntityManagerFactory("mysqlPU").createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
entityManager.close();
please let me know if you need any another information
Is this a case of polyglot persistence or both the entities Person and Address are intended for RDBMS only?
Please share entity definition for Person and Address as well.
-Vivek
Generally for such errors, defining cascading startegy would solve the issue.(e.g. Cascade.ALL, Cascade.PERSIST)
-Vivek
we have used Cascade.ALL.. Please find below Entity Definition for Person and Addres
import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.NamedNativeQuery; import javax.persistence.OneToOne; import javax.persistence.Table;
@Entity @Table(name="PERSON", schema="hibernatepoc") public class Person {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="PERSON_ID" ,unique = true, nullable = false) private int personId;
@Column(name="PERSON_NAME") private String personName;
@Column(name="PERSON_AGE") private int age; @OneToOne(cascade = CascadeType.ALL,targetEntity=Address.class) @JoinColumn(name="ADDRESS_ID") private Address address;
public Person(){
}
public Person(String personName, int age) { super(); this.personName = personName; this.age = age; }
public int getPersonId() { return personId; }
public void setPersonId(int personId) { this.personId = personId; }
public String getPersonName() { return personName; }
public void setPersonName(String personName) { this.personName = personName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public void setAddress(Address address) { this.address = address; }
public Address getAddress() { return address; }
}
import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
@Entity @Table(name="ADDRESS",schema="hibernatepoc") public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ADDRESS_ID",unique = true, nullable = false)
private int addressId;
@Column(name = "STREET")
private String street;
@OneToOne(mappedBy="address",targetEntity=Person.class)
private Person person;
public Address() {
super();
}
public Address(String street) {
super();
this.street = street;
}
public void setStreet(String street) {
this.street = street;
}
public String getStreet() {
return street;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public Integer getAddressId() {
return addressId;
}
@Override
public String toString() {
return "Address [addressId=" + addressId + ", street=" + street
+ ", person=" + "]";
}
public void setPerson(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
}
}
we have tried both Cascade.ALL, Cascade.PERSIST but issues is not resolved yet.
Hi, Please change Person entity for :
@OneToOne(cascade = CascadeType.ALL,targetEntity=Address.class)
@JoinColumn(name="ADDRESS_ID")
private Address address;
with
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="ADDRESS_ID")
private Address address;
Please see if it works for you.
-Vivek
Hi, I have tried but its not work.
KK, I am not around at my workstation, please look this on priority. Assigning to you.
-Vivek
There seems to be an issue with primary key generation (when I remove Auto generated annotation, it works fine)
We are working on it.
Sincerely, Amresh
On Mon, Apr 8, 2013 at 5:07 PM, Vivek Mishra notifications@github.comwrote:
KK, I am not around at my workstation, please look this on priority. Assigning to you.
-Vivek
— Reply to this email directly or view it on GitHubhttps://github.com/impetus-opensource/Kundera/issues/236#issuecomment-16045561 .
OK, But we need auto generation primary key .
Just to update on this:
Kundera relies upon Hibernate for RDBMS persistence using stateless session, With auto id generation hibernate implicitly modifies entity instance for "id" attribute and returns the clone of entity instance. Hence Kundera needs to explicitly modify entity instance in such scenarios. We are working on it and will soon provide an update on this.
-Vivek
Fixed and releasing with 2.6. Please verify. -Vivek
Hi,
I am using Kundera 2.3.1. I have tried to add data into mysql db which has one to one relationship but i am not able to save it..
It throws org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing exception...
PN: We are trying to add auto incremental id.
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SpringExampleDAO springExampleDAO = (SpringExampleDAO)context.getBean("springExampleDAO");