1

I can't seem to add values to my table. There is no error but the values aren't inserting in my database. After submitting the form, no errors would show up.

Upon looking at the console, this shows:

Hibernate: 
    insert 
    into
        referral
        (address, doctor_contact_no, doctor_name, facility_contact_no, facility_type, referral_no, referring_from, referring_to) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)

I already tried logging, just in case my controller can't read the form i'm submitting.

Code Snippet of the Entity/Model i am saving

@Entity
@Table(name="referral")
public class Referrals {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column
    private String referral_no;
    @Column
    private String facility_contact_no;
    @Column
    private String referring_from;
    @Column
    private String referring_to;
    @Column
    private String facility_type;
    @Column
    private String address;
    @Column
    private String doctor_name;
    @Column
    private String doctor_contact_no;' 

Code Snippet of my Services Class

@Service
@Transactional
public class ReferralServicesImpl implements ReferralServices{
    @Autowired
    private ReferralDao referralDao;

    public List<Referrals> list(){
        return referralDao.list();
    }
    public boolean saveReferral(Referrals referral){

        if(referralDao.saveReferral(referral))
            return true;
        else
            return false;
    }
}

Code Snippet of the Controller Methods

@RequestMapping(value = "/teleaudiology/referral", method = RequestMethod.GET)
        public ModelAndView showForm() {
            return new ModelAndView("referrals", "referrals", new Referrals());
        }

        @RequestMapping(value = "/teleaudiology/referrals", method = RequestMethod.POST)
        public String submit(@Valid @ModelAttribute("referrals")Referrals referral, 
          BindingResult result, ModelMap model) {
            if (result.hasErrors()) {
                return "error";
            }
            System.out.println("referral: "+referral.getDoctor_name());
            if(referralServices.saveReferral(referral))
                return "redirect:../teleaudiology";
            else 
                return "redirect:../teleaudiology";
        }

Here is the ReferralDao Class

public interface ReferralDao {
    public boolean saveReferral(Referrals referral);
    public List<Referrals> list();
}

ReferralDao impl

@Repository
@Transactional
public class ReferralDaoImpl implements ReferralDao {
    @Autowired
    SessionFactory session;
    Transaction trans;

    public boolean saveReferral(Referrals referral) {
        // TODO Auto-generated method stub
        //System.out.println("Saving..."+referral.getReferring_to_address());

        trans=session.getCurrentSession().beginTransaction();
        session.getCurrentSession().saveOrUpdate(referral);
        return true;
    }

    public List<Referrals> list() {
        // TODO Auto-generated method stub
        return session.getCurrentSession().createQuery("from Referrals").list();
    }

}

i tried using

trans=session.getCurrentSession().getTransaction();

but resulted to this error

`saveOrUpdate is not valid without active transaction`

Snippet from servlet-context.xml

<tx:annotation-driven transaction-manager="transactionManager" />
    <beans:bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>
  • can u share your ReferralDao class ? – shubham Mar 26 '17 at 09:23
  • hi @shubham added the referralDao class – jayjay-arphie Mar 26 '17 at 09:31
  • Don't call beginTransaction(). Your method is transactional, and Spring handles the transaction for you. Also, respect the Java naming conventions, and don't use the plual form for something which should be singular (Referrals, ReferralServices). Finally, your DAO saveReferral method should return void. This boolean always-true return value is useless and confusing. – JB Nizet Mar 26 '17 at 09:37
  • So, should i just have "trans = session.getCurrentSession();"??? @JBNizet – jayjay-arphie Mar 26 '17 at 09:54
  • i tried using `trans=session.getCurrentSession().getTransaction();` but resulted to this error `saveOrUpdate is not valid without active transaction` @JBNizet – jayjay-arphie Mar 26 '17 at 09:59
  • You don't need to call getTransaction() either. Your beans are annotated with @Transactional. So Spring handles the transaction. You don't need to do anything, provided your transaction configuration is correct (but you didn't post it). – JB Nizet Mar 26 '17 at 10:02
  • Maybe you need extra commit() for beginTransaction(). – utkusonmez Mar 26 '17 at 10:07
  • sorry for being a noob. i added something about transaction. that is the only transaction configuration in the project. @JBNizet – jayjay-arphie Mar 26 '17 at 10:08
  • remove transaction annotation and that session.getCurentSession.getTransaction() and they give a try – surya Mar 26 '17 at 14:36
  • Can you explain how that will work? @surya – jayjay-arphie Mar 26 '17 at 14:39
  • and hope you have enabled automated txn in spring. In configuration class you should have @EnableTransactionManagement . Let me know if it works. i ll explain – surya Mar 26 '17 at 14:42
  • @surya i removed it like you said and received an error. Cannot use saveOrUpdate without any transaction. I used xml configuration, the configuration is in my post. Can you take a look at it? – jayjay-arphie Mar 26 '17 at 15:00
  • You r using a nested transaction here . One in service and another in dao.thus I asked to remove and try. Just use one transaction and did u enable spring tx with annotation I just said – surya Mar 26 '17 at 15:01
  • i am still a newbie so please help. I have something like this. ` ` – jayjay-arphie Mar 26 '17 at 15:05
  • @surya based on a few searches. that snippet is the xml equivalent of `@EnableTransactionManagement` – jayjay-arphie Mar 26 '17 at 15:15
  • BTW, values still not inserting to db. – jayjay-arphie Mar 26 '17 at 15:22
  • is it connected to appropriate database, does any read query gives back any result ! – surya Mar 26 '17 at 15:25
  • yes it is. I am able to do a login. – jayjay-arphie Mar 26 '17 at 15:36

1 Answers1

0

try using below code

 Session s = sessionFactory.getCurrentSession();
 s.save(object);
Radhika
  • 151
  • 4
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Mar 28 '17 at 21:21