• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

org.hibernate.PropertyValueException: not-null property references a null or transient value:

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm new to JPA with hibernate, I have tables with one-to-many relationship and Entity bean accordingly. When im trying to insert the record into the DB, it is throws an exception:
org.hibernate.PropertyValueException: not-null property references a null or transient value:

CREATE TABLE IF NOT EXISTS wsrdatabase.wsr
(
wsr_id integer(100) not null auto_increment,
employee_num varchar(100) not null,
from_date datetime not null,
to_date datetime not null,
status integer default 0 not null,
constraint primary key (wsr_id),
constraint foreign key (employee_num)
references wsrdatabase.employee(employee_num)
on update cascade
on delete cascade
);

CREATE TABLE IF NOT EXISTS wsrdatabase.tasks
(
task_id integer(75) not null auto_increment,
task_name varchar(75) not null,
task_details varchar(1000),
date datetime not null,
time_spent varchar(100) not null,
constraint primary key (task_id)
);

CREATE TABLE IF NOT EXISTS wsrdatabase.tasks_for_the_week
(
wsr_id integer(100) not null,
task_id integer(75) not null,
constraint primary key (wsr_id, task_id),
constraint unique key (task_id),
constraint foreign key (wsr_id)
references wsrdatabase.wsr(wsr_id),
constraint foreign key (task_id)
references wsrdatabase.tasks(task_id)
);

entity bean:
.
.
@Entity
@Table(name = "wsr")
public class WSR
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "wsr_id", nullable = false, unique = true)
private Long wsrId;

@Basic
@Column(name = "employee_num", nullable = false, unique = false)
private String employeeNum;

@Basic
@Column(name = "from_date", nullable = false, unique = false)
private Date fromDate;

@Basic
@Column(name = "to_date", nullable = false, unique = false)
private Date toDate;

@Basic
@Column(name = "status", nullable = false, unique = false)
private Integer status;

@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name = "tasks_for_the_week", joinColumns = {
@JoinColumn(name="wsr_id", unique = true)},
inverseJoinColumns = {@JoinColumn(name="task_id")})
private List<Task> tasks;
.
.
.

@Entity
@Table(name = "tasks")
public class Task
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "task_id", nullable = false, unique = true)
private Long taskId;

@Basic
@Column(name = "task_name", nullable = false, unique = true)
private String taskName;

@Basic
@Column(name = "wsr_id", nullable = false, unique = false)

private String wsrId;

@Basic
@Column(name = "task_details", nullable = true, unique = false)
private String taskDetails;

@Basic
@Column(name = "date", nullable = false, unique = false)
private Date date;

@Basic
@Column(name = "time_spent", nullable = false, unique = false)
private String timeSpent;

@ManyToOne(optional=true)
@JoinTable(name = "tasks_for_the_week", joinColumns = {
@JoinColumn(name="task_id") }, inverseJoinColumns = {
@JoinColumn(name="wsr_id") })
private WSR wsr;
.
.
.
main class

WSR wsr = new WSR();
Task t1 = new Task();
Task t2 = new Task();
wsr.setEmployeeNum( "131408" );
//wsr.setWsrId( new Long("123") );
wsr.setFromDate( new Date() );
wsr.setToDate( new Date() );
wsr.setStatus( new Integer("1") );
t1.setTaskDetails( "XYZ" );
t2.setTaskDetails( "ABC" );
t1.setTaskName("CR");
//t1.setTaskId( new Long ("1") );
//t2.setTaskId( new Long ("2") );
t2.setTaskName( "VTP" );
t1.setDate( new Date() );
t2.setDate( new Date() );
t1.setTimeSpent( "5.0" );
t2.setTimeSpent( "5.0" );
List<Task> a = new ArrayList<Task>();
a.add( t1 );
a.add( t2 );
wsr.setTasks( a );

LOG.info( "Inserting an wsr onject into DB" );
GeneralUtil.checkIfNull( wsr, "WSR " );
EntityManagerFactory entityManagerFactory = Persistence.
createEntityManagerFactory( Constants.PERSISTENCE_UNIT );
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
try
{
entityManager.merge( wsr );
entityTransaction.commit();
}
catch (EntityExistsException eee)
{
LOG.error( "The entity already exists" );
throw new WSRException( "The entity already exists", eee );
// entityTransaction.rollback();
}
catch (Exception e)
{
LOG.error( "Failed to insert into DB" );
throw new WSRException( e.getMessage(), e );
}
finally
{
LOG.debug( "Closing Entity Manager" );
entityManager.close();
}

im getting :
Hibernate: insert into wsr (employee_num, from_date, status, to_date) values (?, ?, ?, ?)
com.wipro.sms.automatedwsr.wsr.exception.WSRException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.wipro.sms.automatedwsr.wsr.model.Task.wsrId
at com.wipro.sms.automatedwsr.wsr.WSRServiceImpl.addWSR(WSRServiceImpl.java:46)
at com.wipro.sms.automatedwsr.wsr.WSRServiceTest.addWSR(WSRServiceTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)>>> org.hibernate.PropertyValueException: not-null property references a null or transient value: com.wipro.sms.automatedwsr.wsr.model.Task.wsrId

at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.wipro.sms.automatedwsr.wsr.model.Task.wsrId
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:244)
at com.wipro.sms.automatedwsr.wsr.WSRServiceImpl.addWSR(WSRServiceImpl.java:34)
... 17 more
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.wipro.sms.automatedwsr.wsr.model.Task.wsrId
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:130)
at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:43)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:238)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:714)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:696)
at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:268)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:319)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:265)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascade(Cascade.java:153)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:479)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:246)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:81)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:704)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:688)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:692)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235)
... 18 more


please help me on this
 
surendra harikantra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it got resolved. I have added one parmeter called wsr id which was not required.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic