Mini-Project - Java Full Stack Developer - JPA_FP Hands-On Solutions | TCS Fresco Play

0

Mini-Project - Java Full Stack Developer - JPA_FP Hands-On Solutions | TCS Fresco Play

Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.

Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).

All Question of the MCQs Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best! 

Mini-Project - Java Full Stack Developer - JPA_FP Hands-On 

The Course Id is 63715.


1. JPADemo.java


 package frescojpa;


import javax.persistence.*;

import frescojpa.Student;


import java.util.*;


public class JPADemo {


  public static void main(String[] args) {

  

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/students.odb");  //creating an entitymanagerfactory instance with the given database

        EntityManager em=emf.createEntityManager();  //enitity manager object creation

          

        em.getTransaction().begin();  //starting the transactions

          

        Student s1=new Student();             //creating 3 student objects

        Student s2=new Student();

        Student s3=new Student(); 

        

        s1.setStudentName("Ramesh"); //setting the names of 3 students as given in question

        s2.setStudentName("Ali");

        s3.setStudentName("John");

          

        em.persist(s1);  //storing them into database

        em.persist(s2);  

        em.persist(s3);    

        em.getTransaction().commit(); //committing the transactions

        List<Student> students= em.createQuery("Select st from Student st").getResultList(); //fetching the students data from database

        //System.out.print("size is:"+students.size());

        for(int i=0;i<students.size();i++){

        System.out.println(students.get(i).studentName+"'s roll number is "+students.get(i).studentRollNumber);


        }

  }


}



2. Student.java


package frescojpa;


import java.io.Serializable;

import javax.persistence.*;


@Entity  //defining student class as the entity class

public class Student implements Serializable {

  

  @Id

  @GeneratedValue(strategy=GenerationType.AUTO) // primary key generation as auto generated

  int studentRollNumber; 

  String studentName;

  Student(){ //default constructor

    

  }

  Student(int studentRollNumber,String studentName){ //parameterized constructor

    this.studentName = studentName;

    this.studentRollNumber=studentRollNumber;

  }


//getters and setters

  public String getStudentName() {

    return studentName;

  }


  public void setStudentName(String studentName) {

    this.studentName = studentName;

  }


  public int getStudentRollNumber() {

    return studentRollNumber;

  }


  public void setStudentRollNumber(int studentRollNumber) {

    this.studentRollNumber = studentRollNumber;

  }

}


**************************************************
Credit for the above notes, goes to the respective owners. 

If you have any queries, please feel free to ask on the comment section.
If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.

Please share and support our page!

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !

Join Telegram Channel

Join MNCAnswers To Get Instant Updates

Join Telegram Channel