简体   繁体   中英

I need this SQL statement in JPA

Is it possible to delete multiple rows via JPA in a database table via a specific ID?

eg

Delete from PERSON_LANGUAGE where PERSON_ID = 125;

Thanks in advance!

Down below is what im currently trying to use without any luck!

public void deletePersonLanguageById(PersonLanguage personLanguage){
    PersonLanguage personLanguage1 = em.find(PersonLanguage.class, personLanguage.getPersonId());
    em.remove(personLanguage1);
}

I want it to delete everything in my databasetable where the ID = 1

You can use a native query to execute SQL directly against your database

Query q = em.createNativeQuery("DELETE FROM person_language WHERE person_id = 1");
q.executeUpdate();

or a JPQL query:

Query q = em.creteQuery("DELETE FROM Person p WHERE p.personId = 1");
q.executeUpdate();

You can create a custom repository that extends from the JPA Repository and there you add the deleteByPersonId(String personId) .

Some more examples here: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM