0

I have class City which is:

@Entity
@Table(name="city",schema="rezervisi")
public class City {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
private int id;
@Getter
@Setter
@NotBlank(message = "Name is mandatory")
private String name;
}

Now I want to override save method in which I send object city. How can I do it if i want to send whole object and not @Params separately. And also since ID is generated how I handle it through parameter here?

Code for repository class is:

public interface CityRepo extends JpaRepository<City,Integer> {

@Query(value="select id, name from city where id = :id ", nativeQuery = true)
Optional<City> findById(Integer id);

@Query(value="select id, name from city where name = :name ", nativeQuery = true)
City findByName(String name);

@Modifying
@Query(value = "insert into city (id,name) VALUES (:id,:name)", nativeQuery = true)
City save(City city ); // instead City save(@Param("id) int id .... 

void delete(City city);
}
1

1 Answer 1

0
  1. You can try using repository.save(city) from your service if it is a CrudRepository.
  2. If that is not what you need for some reason, you can try this out:
    @Modifying
    @Transactional
    @Query(value = "insert into city (id, name) VALUES (?, ?)", nativeQuery = true)
    City save(Long id, String name);
    
    default City save(City city) {
        return save(city.getId(), city.getName());
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.