I want to actually understand the difference between Propagation.NESTED VS
Propagation.REQUIRES_NEW.. My requirement is to save the updateStudent and updateSchool and to rollback the updateBus method where i made runtime exception purposefully. so i have used Propagation.NESTED and tested, which works fine, but the result is same with or without Propagation.NESTED in updateBus method, so what is the purpose of NESTED here.
On other note when i change my code like this without updateStudentInner method, entire transaction is roll backed. So here i don't understand the difference in keeping the Transactional level in updateStudentInner vs updateStudentDetail
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStudentDetail(String name, int id) {
updateStudent(name,id);
updateSchool(name, id);
updateBus(name, id);
}
Here is the entire code for this
package com.example.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
#Service
public class ExampleDaoImpl {
#Autowired
JdbcTemplate jdbcTemplate;
public void updateStudentDetail(String name, int id) {
updateStudentInner(name, id);
}
#Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStudentInner(String name, int id) {
updateStudent(name, id);
updateSchool(name, id);
updateBus(name, id);
}
public void updateStudent(String name, int id) {
String query = "UPDATE STUDENT SET NAME=? WHERE ID=?";
name = "Name " + name;
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateStudent-->" + i);
}
public void updateSchool(String name, int id) {
name = "School " + name;
String query = "UPDATE SCHOOL SET NAME=? WHERE ID=?";
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateSchool-->" + i);
}
#Transactional(propagation = Propagation.NESTED)
public void updateBus(String name, int id) {
name = "Bus " + name;
String query = "UPDATE BUS SET AME=? WHERE ID=?";
int i = jdbcTemplate.update(query, name, id);
System.out.println("i updateBUs-->" + i);
}
}
Also it would be better is someone explain with realtime example of the difference between REQUIRE_NEW, REQUIRED, NESTED and where these to be used.
Related
for instance, I have a table with different locations and their description too. Now I need to print the locations which have descriptions on one page or in one box and simultaneously the locations which do not have descriptions should be displayed on another page.
NOTE:
this has to be done by using spring concept only
for reference, I was able to print the locations which have no descriptions in the below fashion.
entity
package com.test.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
#Entity
#Table(name= "loc_dtls")
public class Location {
#Id
#GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY)
private long id;
#Column(name="place_name")
private String placeName;
#Column(name="place_description")
private String placeDesc;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
public String getPlaceDesc() {
return placeDesc;
}
public void setPlaceDesc(String placeDesc) {
this.placeDesc = placeDesc;
}
#Override
public String toString() {
return "Location [id=" + id + ", placeName=" + placeName + ", placeDesc=" + placeDesc + "]";
}
}
controller
#GetMapping("/")
public String singleElement(Model m) {
List<Location> util = locRepo.findByplaceDesc(null);
m.addAttribute("unlist_places", util);
return "index";
}
repository
#Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
public List<Location> findByplaceName(String placeName);
public List<Location> findByplaceDesc(String placeDesc);
}
html
<select>
<option>--select--</option>
<option th:each="p : ${unlist_places}"
th:text="${p.placeName}">
</option>
</select>
MySql Database
I am trying to add default values to property attributes.I have one class inside which i have other class type injected as list.
I am able to get the default values for all attributes even on dependent class.I want to know is there any way using #value to add one more list of default values of custom objects.
My model classes are-
package com.example.test.Model;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Employee {
#Value("1")
private Integer id;
#Value("Anubham")
private String name;
#Autowired
private List<Departments>departments;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Departments> getDepartments() {
return departments;
}
public void setDepartments(List<Departments> departments) {
this.departments = departments;
}
public Employee() {
super();
}
public Employee(Integer id, String name, List<Departments> departments) {
super();
this.id = id;
this.name = name;
this.departments = departments;
}
#Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", departments=" + departments + "]";
}
}
Another one is:
package com.example.test.Model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Departments {
#Value("1")
private int id;
#Value("computer")
String subject;
public Departments() {
super();
}
public Departments(int id, String subject) {
super();
this.id = id;
this.subject = subject;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
#Override
public String toString() {
return "Departments [id=" + id + ", subject=" + subject + "]";
}
}
I am getting output as Employee [id=1, name=Anubham, departments=[Departments [id=1, subject=computer]]].
I want to have one more record for departments field.
I wonder is it possible using #value without using any other way.
In your example "Departments" is a bean that is injected into Employee bean. If you want to have multiple departments, you have to create interface/abstraction "Department" and implement it in beans with concrete values that you want (DepartmentA, DepartmentB).
But Value annotation is not meant do inject static content but rather values from properties files. I don't know what you want to achieve this way.
I am currently working on a Spring Boot Hibernate.
I need to save a list of objects in mySql database,
I read about the difference between saving using saveAll() and foreach save() and I can conclude that saveAll() is much faster. However, in case of exception say UK violation for example, the whole process rollback, and no object will be saved.
To be more simple: I want to save a list of object in one transaction, and in case of any constraint violation, ignore it and save the rest.
this is my entity:
package com.entity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
#Transactional
public class Page {
#Id
#GeneratedValue(generator = "page_sequence-generator")
#GenericGenerator(
name = "page_sequence-generator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
#Parameter(name = "sequence_name", value = "page_sequence"),
#Parameter(name = "initial_value", value = "1"),
#Parameter(name = "increment_size", value = "1")
}
)
private long id;
#Column(nullable = false, unique = true)
private String url;
private boolean isPageConsumed;
public Page() {
}
public Page(String url, boolean isPageConsumed) {
this.url = url;
this.isPageConsumed = isPageConsumed;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isPageConsumed() {
return isPageConsumed;
}
public void setPageConsumed(boolean pageConsumed) {
isPageConsumed = pageConsumed;
}
#Override
public String toString() {
return "Page{" +
"id=" + id +
", url='" + url + '\'' +
", isPageConsumed=" + isPageConsumed +
'}';
}
}
And this is my repository:
#Repository
public interface PageRepository extends CrudRepository<Page, String> {
#Query(value = "SELECT p FROM Page p where p.isPageConsumed = '0'")
public Page findFirstPage();
}
Thank you :)
You could use Spring's #Transactional(noRolbackFor={SomeException.class}) on the controller method that invokes saveAll().
I used SpringBoot, and in the PUT method I check if the score exists then I want to update the score and also update the history by adding the latest score to it.
The Score Class:
package thesisMongoProject;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonView;
#Document(collection = "score")
public class Score {
#Id
#NotBlank
#JsonView(Views.class)
private String score;
#NotBlank
#JsonView(Views.class)
private String player;
#NotBlank
#JsonView(Views.class)
private String code;
#JsonView(Views.class)
private Date date;
private List<History> history;
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
#Override
public String toString() {
return "Score [score=" + score + ", player=" + player + ", code=" + code + ", history=" + history + ", date="
+ date + "]";
}
}
The ScoreRepository:
package thesisMongoProject.Repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import thesisMongoProject.Score;
import thesisMongoProject.ScoreDto;
#Repository
public interface ScoreRepository extends MongoRepository<Score, String>{
public Score findByScore(String score);
public void save(ScoreDto scoredto, String score);
}
But the PUT method save a new instance into the MongoDB instead of updating the existing one
The PUT method:
//Update Score By ID
#PutMapping("/{score}")
public ResponseEntity<?> updatePlayerByID(
#PathVariable("score")String score,
#RequestBody #JsonView(Views.class) #Valid Score score1){
Score findscore = srepo.findByScore(score);
if(findscore == null)
return ResponseEntity.status(404).body("There is not Score!");
else {
history = new ArrayList<History>();
h = new History();
h.setScore(score1.getScore());
h.setDate(score1.getDate());
history.add(h);
score1.setHistory(history);
srepo.save(score1);
return ResponseEntity.ok(score1);
}
}
Also i tried to use ScoreDTO and #PatchMapping like this:
The ScoreDTo Class:
package thesisMongoProject;
import java.util.List;
public class ScoreDto {
private String score;
List<History> history;
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public List<History> getHistory() {
return history;
}
public void setHistory(List<History> history) {
this.history = history;
}
}
And the PATCHMAPPING:
#PatchMapping("/{score}")
public ResponseEntity<?> updateByScore(
#PathVariable("score")String score,
#RequestBody ScoreDto score1){
Score findscore = srepo.findByScore(score);
if(findscore == null)
return ResponseEntity.status(404).body("There is not Score!");
else {
srepo.save(score1, score);
return ResponseEntity.ok(score1);
}
}
but in my console I have an error:
org.springframework.data.mapping.PropertyReferenceException: No property save found for type Score! Did you mean 'date'?
could you help me how can i update the existing field of score, please?!
The primary key of a database should not be mutable. If there are multiple players with the same score, the earlier players' data would be replaced.
Ideally, for updating an existing document where id and all its new fields are known, something like this can be done:
score1.setScore(score);
srepo.save(score1);
Assuming score is the id of the document that is to be updated and score1 contains all other fields correctly, this will replace the existing document with id score with the new one score1.
In the first code ( the PUT method ), score1 should have the same id as findscore, then it will update the existing document.
Score findscore = srepo.findByScore(score);
if(findscore == null)
return ResponseEntity.status(404).body("There is not Score!");
else {
history = new ArrayList<History>();
h = new History();
h.setScore(score1.getScore());
h.setDate(score1.getDate());
history.add(h);
Also, for the exception you are getting, this save method
public void save(ScoreDto scoredto, String score);
can't be handled by the spring data repository automatically, you will have to define its implementation. More on what kind of methods can be defined or not here. The Standard save method in the repository can be used to achieve the required.
I am using Spring's SimpleJdbcInsert class to create entities - eg:
final SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("abc");
insert.execute(new BeanPropertySqlParameterSource(abc));
Is there some equivalent of this class for doing updates? As an example, something like the below would be a convenient interface, assuming we are dealing with a single column primary key:
final SimpleJdbcUpdate update = new SimpleJdbcUpdate(dataSource).withTableName("abc").withIdColumn("abcId");
update.execute(new BeanPropertySqlParameterSource(abc));
Does Spring provide this functionality out-of-the-box somewhere?
Thanks
Jay
For any future readers - I came up with a convenience function using reflection;
Works for simple pojos:
public void dao_update(NamedParameterJdbcTemplate database, String table, Object pojo, String[] keys) {
StringBuilder sqlBuilder = new StringBuilder("UPDATE ");
sqlBuilder.append(table);
sqlBuilder.append(" SET ");
boolean first = true;
for (Field field : pojo.getClass().getDeclaredFields()) {
if (!first) {
sqlBuilder.append(",");
}
first = false;
sqlBuilder.append(field.getName());
sqlBuilder.append(" = :");
sqlBuilder.append(field.getName());
}
first = true;
for (String key : keys) {
if (first) {
sqlBuilder.append(" WHERE ");
} else {
sqlBuilder.append(" AND ");
}
first = false;
sqlBuilder.append(key);
sqlBuilder.append("= :");
sqlBuilder.append(key);
}
database.getJdbcOperations().update(sqlBuilder.toString(), new BeanPropertySqlParameterSource(pojo));
}
Example usage:
dao_update(database, "employee", my_employee, "id");
Generates:
UPDATE employee SET id = :id, name = :name, salary = :salary WHERE id = :id
There is an issue in the Spring JIRA about the lack of a SimpleJdbcUpdate class: https://jira.springsource.org/browse/SPR-4691. You might want to upvote it there.
You have to use JdbcTemplate
See: 13.2.1.1 Examples of JdbcTemplate class usage
E.X:
this.jdbcTemplate.update(
"update t_actor set = ? where id = ?",
"Banjo", 5276L);
You can get more similar effect by using SimpleJdbcTemplate instead of JdbcTemplate and by extending SimpleJdbcDaoSupport all DB operations can be put in one DAO class:
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
#Repository
public class BankDaoImpl extends SimpleJdbcDaoSupport implements BankDao {
#Autowired
public BankDaoImpl(#Qualifier("dataSource") DataSource dataSource) {
setDataSource(dataSource);
}
#Override
public void insert(Bank bank) {
String sql = "INSERT INTO BANK (id, oib, short_name, name, street, town, postal_code, homepage_url, last_change) VALUES (NEXT VALUE FOR bank_seq, :oib, :shortName, :name, :street, :town, :postalCode, :homepageUrl, CURRENT_TIMESTAMP)";
SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(
bank);
getSimpleJdbcTemplate().update(sql, parameterSource);
}
#Override
public void update(Bank bank) {
String sql = "UPDATE BANK SET oib=:oib, short_name=:shortName, name=:name, street=:street, town=:town, postal_code=:postalCode, homepage_url=:homepageUrl, last_change=CURRENT_TIMESTAMP WHERE id=:id";
SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(
bank);
getSimpleJdbcTemplate().update(sql, parameterSource);
}
#Override
public void delete(String id) {
String sql = "DELETE FROM BANK WHERE id=:id";
getSimpleJdbcTemplate().update(sql,
new MapSqlParameterSource("id", id));
}
#Override
public Bank findById(String id) {
String sql = "select b.ID, b.OIB, b.SHORT_NAME, b.NAME, b.STREET, b.TOWN, b.POSTAL_CODE, b.HOMEPAGE_URL, b.LAST_CHANGE, CASE WHEN count(f.id) = 0 THEN 0 ELSE 1 END AS ready " +
"from BANK WHERE b.ID = :id";
return getSimpleJdbcTemplate().queryForObject(sql,
BeanPropertyRowMapper.newInstance(Bank.class),
new MapSqlParameterSource("id", id));
}
}
The easy way to do this is:(source)
public void setName(int id, String name) {
this.jdbcTemplate.update("update mytable set name = ? where id = ?",
new Object[] {name, new Integer(id)});
}