Hello Everyone,
I have two tables Account and Account User. Account User is the child element in the Account table. And Account_Id is the Foreign Key in the Account User. And I am using Sequence generators to generate the primary keys in both the tables . My Account and Account User entities look like this.
My Environment : Spring Integrated with Hibernate, Java 1.6.
import java.io.Serializable; import java.sql.Timestamp; import java.util.HashSet; import java.util.Set;
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.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table;
@Entity @Table(name = "ACCOUNT") public class Account implements Serializable { private static final long serialVersionUID = 1L;
@Id @SequenceGenerator(name = "ACCOUNT_ID_SEQ_GEN", sequenceName = "INTERNAL_ID_SEQ") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ACCOUNT_ID_SEQ_GEN") @Column(name = "ACCOUNT_ID") private Long accountId;
@Column(name = "DOMAIN_ID", nullable = false) private Long domainId;
@Column(name="STATUS",nullable=false) private String status;
@Column(name = "DISPLAY_NAME", nullable = false,length=256) private String displayName;
@Column(name = "USER_COUNT", nullable = true) private Integer userCount;
@Column(name = "FULL_USER_COUNT", nullable = true) private Integer fullUserCount;
@Column(name = "ACTIVE_USER_COUNT", nullable = true) private Integer activeUserCount;
@Column(name = "CREATED_DATE", nullable = false) private Timestamp createdDate;
@ManyToOne @JoinColumn(name = "CREATED_BY", nullable = false) private Node createdByNode;
@Column(name = "UPDATED_DATE", nullable = true) private Timestamp updatedDate;
@ManyToOne @JoinColumn(name = "UPDATED_BY", nullable = true) private Node updatedByNode; @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="account") private Set<AccountUser> accountUsers = new HashSet<AccountUser>(0); /* generated setter and getter methods */ }
import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; import java.util.HashSet; import java.util.Set;
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.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient;
@Entity @Table(name = "ACCOUNT_USER") public class AccountUser implements Serializable { private static final long serialVersionUID = 1L;
@Id @SequenceGenerator(name = "USER_ID_SEQ_GEN", sequenceName = "INTERNAL_ID_SEQ") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID_SEQ_GEN") @Column(name = "USER_ID") private Long userId;
// FK to ACCOUNT @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name = "ACCOUNT_ID", nullable = false) private Account account;
@Column(name="STATUS",nullable=false) private String status;
// FK to USER_PREVILIGE @Column(name="PRIVILEGE",nullable=false) private String privilege;
@Column(name = "USERNAME", nullable = false, unique = true,length=256) private String userName;
@Column(name = "PASSWORD", nullable = false,length=256) private String password;
@Column(name = "DISPLAY_NAME", nullable = false,length=256) private String displayName;
@Column(name = "SORT_NAME", nullable = true,length=256) private String sortName;
@Column(name = "FIRST_GIVEN_NAME", nullable = true,length=64) private String firstGivenName;
@Column(name = "SECOND_GIVEN_NAME", nullable = true,length=64) private String secondGivenName;
@Column(name = "FAMILY_NAME", nullable = true,length=64) private String familyName; /* Generated Getter and Setter methods */
}
when I try to insert a row into the ACCOUNT table (which should also insert a row in the ACCOUNT_USER table) using the following code I get the following Exception.
getHibernatetemplate().save(account);
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.deme.db.hibernate.entity.AccountUser.account; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.deme.db.hibernate.entity.AccountUser.account at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:645) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748) at com.deme.db.hibernate.impl.AccountDaoImpl.createAccount(AccountDaoImpl.java:29) at com.deme.db.hibernate.AccountDaoTestCase.testCreateAccount(AccountDaoTestCase.java:107) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.deme.db.hibernate.entity.AccountUser.account at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527) at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241) at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193) at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:320) at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:266) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:243) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193) at org.hibernate.engine.Cascade.cascade(Cascade.java:154) at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:479) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:357) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523) at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:751) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419) ... 28 more
|