Sample sample = new Sample(); sample.setName("bar"); sample = sampleRepository.save(sample); sampleSearchRepository.save(sample);
public class SampleGSONParserAdapter implements JsonDeserializer<Sample> { @Override public Sample deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Sample sample = new Sample(); JsonObject sampleJsonObject = json.getAsJsonObject(); sample.setName(sampleJsonObject.get("name").getAsString()); // do the other parsing stuff here.. // getting the data object as a string sample.setJsonString(sampleJsonObject.get("data").toString()); return sample; } }
Sample sample = new Sample(); /*call the setter from Sample class and pass value to its parameter*/ sample.setName("Andrew"); /* access the name "Andrew" by calling getter */ System.out.println("Welcome " + sample.getName()); }
public void initialize() { Sample startEntity = new Sample(); startEntity.setId(1); startEntity.setName("Start name"); sampleRepository.saveSample(startEntity); sampleRepository.flush(); // <-- just to make sure scenario is recreated sampleRepository.clear(); // same as above LOGGER.info(sampleRepository.findSampleById(1)); sampleRepository.clear(); // same as above above :D }
@Transactional @Component public class SampleService { private static final Logger LOGGER = Logger.getLogger(SampleService.class); @Autowired SampleRepository sampleRepository; @Transactional (propagation = Propagation.REQUIRES_NEW) public void method1() { Sample someSample = new Sample(); someSample.setId(1); someSample.setName("TestSample before update but after create"); sampleRepository.updateSample(someSample); } public void method2() { method1(); // I need to be sure that data was persisted to DB and find the entity by id Sample someSampleAfterUpdate = sampleRepository.findSampleById(1); //I believe that at that point sample is found in L-1 cache not in db directry. // entity is not updated here LOGGER.info(someSampleAfterUpdate); //in this point, transaction is not over yet, so you wont notice change in database.. } }
@SpringBootApplication public class Application implements CommandLineRunner { @Autowired SampleRepository repository; @Override public void run(String... args) { this.repository.doThings(); } public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Application.class); application.setApplicationContextClass(AnnotationConfigApplicationContext.class); SpringApplication.run(Application.class, args); } public void doThings() { Sample sample = new Sample(); sample.setName("arun"); repository.save(sample); } }