Crazy programmers

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.

29/01/2018

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

20/11/2017

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

20/11/2017

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..???

18/10/2017

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 .

15/09/2017

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.

Interview>Collection>q:Sorting without using defiend methods..šŸ¤£šŸ¤£šŸ¤£šŸ˜†šŸ˜†šŸ˜†šŸ˜† Happy coding
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.

30/04/2017

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.

Address

Delhi

Telephone

9990455579

Website

Alerts

Be the first to know and let us send you an email when Crazy programmers posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to Crazy programmers:

Share