0
저는 JpaRepository를 처음 사용합니다. StudentClient.java 클래스가 있는데, 여기서 Hibernate를 사용하여 레코드를 삽입합니다.JPARepository를 사용하여 엔티티 저장
제 질문은 JpaRepository를 사용하고 싶다면 어떻게해야합니까?
레코드를 삽입하고 Jparepository를 사용하여 엔티티를 저장하려고합니다. 코드-
public class StudentClient
{
public static void main(String[] args) throws Exception
{
// create Configuration class, Configuration object parses and reads .cfg.xml file
Configuration c = new Configuration();
c.configure("/hibernate.cfg.xml");
// SessionFactory holds cfg file properties like, driver props and hibernate props and mapping file
SessionFactory sf=c.buildSessionFactory();
// create one session means Connection
Session s = sf.openSession();
// before starting save(),update(), delete() operation we need to start TX, starting tx mean con.setAutoCommit(false);
Transaction tx = s.beginTransaction();
try
{
Student std1=new Student();
std1.setSid(100);
std1.setSname("S N Rao");
std1.setSmarks(78);
std1.setSjoindate(new Date());
Student std2=new Student();
std2.setSid(101);
std2.setSname("Sumathi");
std2.setSmarks(52);
std2.setSjoindate(new Date());
s.save(std1); // stmt.addBatch("INSERT INTO school VALUES (....)");
s.save(std2);
s.flush(); // stmt.executeBatch()
tx.commit(); // con.commit();
System.out.println("Records inserted");
}
catch(Exception e)
{
tx.rollback(); // con.rollback();
}
}
}
그래서 setter 메서드를 사용하여 레코드를 삽입 한 다음 JPARepository를 사용하여 효율성을 저장할 수 있습니까? –
* 엔티티. 또 다른 질문 : jdbcTemplate과 jparepository를 둘 다 같은 클래스에서 사용할 수 있습니까 ?? 가능한가? jdbcTemplate을 사용하여 특정 DDL 문을 실행해야하며 JpaRepository를 사용하여 레코드를 삽입해야합니다. –
네, 엔터티에 원하는 데이터를 채우고'jpaRepository.save'를 호출하면됩니다. – luboskrnac