Page 1 of 1

New entity not getting committed

Posted: Thu Aug 09, 2012 12:06 pm
by dilupa.m
I followed the wiki and successfully extended Customer entity and created my own. All data saves successfully for the extended entity. I then created a new entity and added its associated Dao and Service layers. The entity and spring configurations for the classes are working without any issues. But it seems like hibernate is not committing my data to the table. the entity manager merge method returns me the persisted object without any problem.

Here's what my Dao class looks like

Code: Select all

@Repository("clResidentDao")
public class ResidentDaoImpl implements ResidentDao {

    @PersistenceContext(unitName="blPU")
    protected EntityManager em;
   
   @Override
   public Resident saveResident(Resident resident) {
      Resident retResident = em.merge(resident);      
      return retResident;
   }

}


And my annotated entity

Code: Select all

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "CLN_RESIDENT", uniqueConstraints = @UniqueConstraint(columnNames = { "RESIDENT_ID" }))
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="blStandardElements")
public class ResidentImpl implements Resident{

   private static final long serialVersionUID = 1L;
   
   /** The id. */
   @Id
        @Column(name = "RESIDENT_ID")
        @GeneratedValue(strategy=GenerationType.IDENTITY)
   private Long id;
   
   /** The contact pref. */
   @Column(name = "CONTACT_PREF")
   private String contactPref;
   
   /** The movin date. */
   @Column(name = "MOVIN_DATE")
   private Date movinDate;
   
   /** The is active. */
   @Column(name = "IS_ACTIVE")
   private Boolean isActive;
   
   /** The is payer. */
   @Column(name = "IS_PAYER")
   private Boolean isPayer;
   
   /** The notes. */
   @Column(name = "NOTES")
   private String notes;
   
   /** The CLN id. */
   @Column(name = "CLN_ID")
   private String CLNId;
   
   /** The Community id. */
   @Column(name = "COMMUNITY_ID")
   private Long CommunityId;
   
   /** The customer. */
   @OneToOne(targetEntity = CLCustomerImpl.class)
        @JoinColumn(name = "CUSTOMER_ID")
   private Customer customer;
   
   /** The payer. */
   @OneToOne(targetEntity = CLCustomerImpl.class)
        @JoinColumn(name = "PAYER_ID")
   private Customer payer;
   
   /** The residents. */
   @ManyToMany(targetEntity = FamilyMemberImpl.class)
     @JoinTable(name = "CLN_RESIDENT_FAMILY", joinColumns = @JoinColumn(name = "RESIDENT_ID"), inverseJoinColumns = @JoinColumn(name = "FAMILY_MEMBER_ID", referencedColumnName = "FAMILY_MEMBER_ID"))
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="blStandardElements")
    private List<FamilyMember> familyMember;


I havent configured unit testing yet. so to test this, i tried to save a mock resident from the RegisterController like this

Code: Select all

@RequestMapping(method=RequestMethod.POST)
   public String processRegister(@ModelAttribute("registrationForm") RegisterCustomerForm registerCustomerForm, BindingResult errors, HttpServletRequest request, HttpServletResponse response, Model model) {
      String url = super.processRegister(registerCustomerForm, errors, request, response, model);
      
      Resident resident = new ResidentImpl();
      resident.setId(new Long(3));
      resident.setCLNId("CLNId123");
      resident.setCommunityId(new Long(123));
      resident.setContactPref("email");
      resident.setCustomer(null);
      resident.setIsActive(true);
      resident.setIsPayer(false);
      resident.setMovinDate(new Date());
      resident.setNotes("New Note");
      resident.setPayer(null);      
      Resident savedResident = residentService.saveResident(resident);
      System.out.println("Saved Resident :" + savedResident.getCLNId());
      return url;
   }

The system out prints the resident data but doesnt actually commit it to the database. Am i missing something here?
Thanks

Re: New entity not getting committed

Posted: Fri Aug 10, 2012 12:36 pm
by jefffischer
Yes, you'll need to either include a @Transactional annotation on your service method, or include an AOP declaration for the transaction demarcation in your application context (either do the same thing). I believe it's not committing because there's not a transaction in place.

Re: New entity not getting committed

Posted: Fri Aug 10, 2012 3:03 pm
by dilupa.m
Thanks. it worked :)