Crazy programmers
Contact information, map and directions, contact form, opening hours, services, ratings, photos, videos and announcements from Crazy programmers, Computer Company, Delhi.
Hi Welcome to crazy programmer page, we are here to discuss about ideas ,innovation ,technology specially we talk about java j2ee , pls like ,comment and subscribe for more information and lets make a healthy discussions on coding.
Any one interested in Spring and hibernate application course , no theory complete practical knowledge , learn while working on our project. For Delhi NCR student . very affordable fees. Just rs 1999 contact 9990455579
class Student{
int id;
String name;
Student getstudent(String stuid) throws Studentnotfoundexception {
System.out.println("get student method");
if(stuid.equals("343")){
return new Student();
}else{
throw new Studentnotfoundexception("Student not fpound:"+stuid);
}
System.out.println("get student method exit");
}
}
public class Customexception {
public static void main(String args[]) {
Student s1=new Student();
try {
System.out.println("Going in try");
s1.getstudent("33");
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("hello");
} catch (Studentnotfoundexception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
System.out.println("finally");
System.out.println();
}
System.out.println("Ajay");
}
}
Output:
1:Compile time Error
2:Runtime Error
3:Going in try=>get student method
Exception.Studentnotfoundexception: =>Student not fpound:33
finally
Ajay
4:Dont know
class Student{
int id;
String name;
Student getstudent(String stuid) {
System.out.println("get student started");
if(stuid.equals("343")){
return new Student();
}else{
try {
throw new Studentnotfoundexception("Student not found:"+stuid);
} catch (Studentnotfoundexception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("get student method");
return null;
}
}
public class Customexception {
public static void main(String args[]) {
Student s1=new Student();
try {
System.out.println("Going in try");
s1.getstudent("33");
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("hello");
}
finally{
System.out.println("finally");
System.out.println();
}
System.out.println("Ajay");
}
}
Guess The correct Output..???
Hello guys , this diwali we are offering a new crash course on core java, hibernate and spring ,mainly focuses on individual having having interest in programming in java. we are offering some seats free , message me why u want to join this course free.? hurry up Limited seats available.đđđđ ,Candidate located in delhi ncr specially indrapuram location can take benefit of this offer .Happy Diwali , keep smiling , keep learning .
Iâm by no means an expert in Hibernate, but I do use it almost every day for my own projects, so I do know a thing or two about how it works.
One topic that had me scratching my head for ages was the Hibernate life cycle. What I mean by the life cycle is the way Hibernate interacts with Java objects at certain points in the existence of said Java objects.
Letâs start from the beginningâŚ
What the heck is a Hibernate Life Cycle?
You see, Hibernate is picky about your Java objects. Hibernate prefers your objects to be in a certain âstateâ, known as the persistent state⌠this persistent state is one of four different states that exist inside of the hibernate persistence life cycle.
Once you have a firm grasp of the different states that an object can be in (as it pertains to Hibernate) youâll be well on your way to mastering the Hibernate framework.
So letâs get this Hibernate persistence life cycle lesson started shall we?
Transient
A transient object is one that Hibernate has no awareness of whatsoever. Itâs the first step on our journey through the life cycle. When you first create a plain old Java object (via the new keyword) this object can be thought of as being in a transient state.
So really, when you think about it, every object youâve ever created (before you started working with Hibernate) could be thought of as being in a transient state. Again, this is the case because the database has absolutely no idea that this object is in existence.
How this âstateâ applies to Hibernate is that Hibernate will not be able to track transient objects and store them in the database (or update the database based on their values).
In order for the database to properly keep track of objects, is for them to be in the next state of the Hibernate persistence life cycle.
Persistent
When an object is in a persistent state, Hibernate is totally aware of it and can keep the database synchronized with itâs values.
Before we launch into the persistent state, letâs talk about how we can go from transient to persistent.
There are two main ways to make the leap from a transient object to a persistent object:
Loading the object from the database via Hibernate APIs
Saving the object to the database via Hibernate APIs
Ways to Save an Object
Hibernate has a few different ways to save an object to the database, but the two main ways are as follows:
session.save()
session.saveOrUpdate()
Invoking either of these Hibernate methods will shift your transient object into the persistent state (so long as the save is successful).
I wonât go into detail about how to use these methods, as Iâve already done so in this post.
Ways to Load an Object
There are quite a few ways to load an object from a database. Hereâs a couple that I use the most:
session.get()
session.createCriteria()
I wonât go into detail here, as Iâve already covered how to load data from a database using Hibernate.
Why the Persistent State is King
Hibernateâs true abilities are shown when working with persistent objects.
When an object is in a persistent state within a transactional context in Hibernate, magical things happen. Namely, Hibernate will be able to synchronize the persistent object(s) when you manually invoke a save or when the transactional context is closed and committed.
To create this âtransactional contextâ I simply rely on the Spring framework and its annotation. Youâve seen this used already in a previous post.
But you can certainly create your own transactional context manually if you do not prefer to use the Spring framework. This just means that youâll be writing some boilerplate transaction management code. Iâm not an expert in this area at all, so if youâre interested in researching it further, there are many articles that cover transactions with Hibernate on the web.
In any case, the point Iâm trying to get at here is that the majority of Hibernateâs âpersistenceâ functionality occurs with your object is in a persistent state.
This means that if you want to add new data to the database, youâll need to invoke the session.save() or session.saveOrUpdate() methods to take your transient object into a persistent state.
Or this means that if you want to update existing data in the database, youâll first need to load it into an object via the aforementioned session.get() or session.createCriteria() methods.
Only when youâre modifying / updating persistent objects will Hibernate actually synchronize your changes from the Java object to your database.
Detached
Okay, so you know that the persistent state is key, but you can only be in this state while youâre inside of a transactional context.
In the world of Spring, this means that you will only be in a persistent state while inside of your DAOâs âsaveâ methods. This is the case because Spring wraps a transactional context around each method in your DAO class. This happens because we used the annotation at the class level.
So what happens one the flow of code leaves your DAOâs save method? Well, the transactional context is closed (via the Spring code I just talked about) and your object that was previously in the persistent state is now in a detached state.
The detached state is given to an object that was previously âattachedâ (persistent) but has now left the scope of the transactional context.
The âside effectâ of this state is that if you make any changes to your object, Hibernate wonât be tracking them and thus wonât synchronize the changes to the database. Youâll need to invoke a session.saveOrUpdate(), a sesssion.update(), or a session.merge() in order to re-attach it and make it persistent again in a new transactional context.
This is where things can get a bit wonky though.
You see, since your object was previously persistent, Hibernate still holds a record of this object in its memory. So when your (now detached) object is passed into a session.update() method, Hibernate tries to update only the records that have changed since it last âknewâ your object.
There are times when Hibernate just doesnât know how to handle the updates and can do nothing except throw exceptions. Iâve hit this scenario before and itâs quite annoying.
Again, Iâm not an expert in this field, so if anyone can explain exactly whatâs going on in this particular scenario, then by all means leave a comment below.
In any case, the solution is to use the session.merge() method instead. What the merge() does is just overwrite everything (thus essentially ignoring whatâs in Hibernateâs memory) with the object that youâre passing into the merge() method.
Removed
Finally, the last state in Hibernateâs persistence life cycle is the âremovedâ state. This happens when you have a persistent object that youâve flagged to be deleted.
To delete a persistent object, you just invoke the session.delete() method and pass in your persistent object.
Note that you cannot properly delete a non-persistent object, so to properly delete an object youâll need to load it using the methods we discussed earlier (going from transient to persistent).
Once youâve deleted an object and moved to the âremovedâ state, you should no longer use that particular object for any reason.
21/06/2017
Interview>Collection>q:Sorting without using defiend methods..đ¤Łđ¤Łđ¤Łđđđđ Happy coding
21/06/2017
Frequently asked Interview Question..
How to create Immutable class solution...below. Happy coding.
class Ajay{
Ajay a3=new Ajay();
Ajay a4=new Ajay();
}
class test{
public static void main(String args[]){
Ajay a1=new Ajay();
Ajay a2=new Ajay();
//System.out.println(a1==a2);
System.out.println(a1.a3==a1.a4);
}
}
Program output showing some thing weird i was expecting it as a runtime error but it run sucessfully output is
:\JavaPrac>The system cannot find the file specified.
'The' is not recognized as an internal or external command,
operable program or batch file.
D:\JavaPrac>
D:\JavaPrac>D:\JavaPrac> at Ajay.(langsix.java:2)
The system cannot find the file specified.
Click here to claim your Sponsored Listing.
Category
Contact the business
Telephone
Website
Address
Delhi