Mar 24

ids for this class must be manually assigned before calling save()

Exception in thread “main” org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save().

Exception in thread “main” org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.hr.hibernate.model.Employee
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.hibernate.util.HibernateConfig.main(HibernateConfig.java:30) Continue reading

Mar 24

Failed.org.hibernate.AnnotationException: No identifier specified for entity

Failed.org.hibernate.AnnotationException: No identifier specified for entity:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.AnnotationException: No identifier specified for entity: com.hr.hibernate.model.Employee
Exception in thread “main” java.lang.ExceptionInInitializerError
at com.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
at com.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at com.hibernate.util.HibernateConfig.main(HibernateConfig.java:25)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.hr.hibernate.model.Employee
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:626)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at com.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
… 2 more Continue reading

Mar 22

org.hibernate.MappingException: Unknown entity

Exception in thread “main” org.hibernate.MappingException: Unknown entity: com.hr.hibernate.model.Employee

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread “main” org.hibernate.MappingException: Unknown entity: com.hr.hibernate.model.Employee
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338) Continue reading

Mar 22

org.hibernate.MappingExceptAn AnnotationConfiguration instance is required to use

Hibernate Exception : An AnnotationConfiguration instance is required to use

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping/>
Exception in thread “main” java.lang.ExceptionInInitializerError
at com.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
at com.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
at com.hibernate.util.HibernateConfig.main(HibernateConfig.java:22)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1597)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at com.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
… 2 more

Solution: Whenever we are using Annotations in our pojo class instead of hbm files we need to use Annotation configuration instead of normal configuration

For normal hbm files : This is how we instantiate Configuration. Continue reading

Mar 08

Java interview questions and answers part 60

296) Explain Bitwise complement operator(~)  in java with example?

Bitwise complement operator This operator is applicable only for Integer operands.When bitwise complement operator is applied to integer operands it returns bitwise complement of an operand.

For example assume that if

Integer i=1.

Integer j=~i;

First convert the value of 1 to binary.Assume that if the binary value of 1 is 0000.Its complement  is 1111.So now the value of j is representation of 1111 is integer format. Continue reading

Mar 08

Java interview questions and answers part 59

291)    Explain Relational operators with example?

Relational operators are used to compare two operands.Relational Operators are used to compare if one operand is greater than ,less than ,equal to ,greater than equal to ,less than equal to and not equal to other operand.

Operator Operation
Greater than
Less than
>= Greater than equal to
<= Less than equal to
== Equal to
!= Not Equal to

 

292)    Explain unary arithmetic operators with example?

Operator Operation
+ Indicates Positive values
Returns Negative Value
++(++a PreIncrement) Increments the value by 1
++(a++ Post Increment) Increments the value by 1
–(–a PreDecrement) Decrements the value by 1
–(a—Post Decrement) Decrements the value by 1
Complement operator (!) Changes the value from true to false and false to true.
Bitwise complement operator(~) It complements the bit value that is it changes bit 1 to o and o to 1


293)    Explain difference between PreIncrement and PostIncrement operators in java?

Both preincrement and post increment value increments the value by 1.

If the operator appears before operand it is called PreIncrement Operator.If the operator appears after operand it is called post Increment operator.

The main difference between PreIncrement and PostIncrement operator comes when result is assigned to an value.

PreFix Increment Operator :

int x=1;

int y=++x;

 Prefix increment operator increments x value by 1 and assigns it to y.Now the value of x is 1 and y is 2.

 PostFix Increment Operator :

int x=1;

Int y=x++;

From the above expression first value is assigned to y so the y value is 1 and then value gets incremented.Now the x value is 2.

After post increment values are x=2,y=1.

 

294)    Explain difference between prefix decrement and postfix decrement operators?

Both predecrement and post decrement operator decrements the value by 1.

If the operator appears before operand it is called PreDecrement Operator.If the operator appears after operand it is called post Decrement operator.

The main difference between PreDecrement and PostDecrement operator comes when result is assigned to an value.

PreFix Decrement Operator :

int x=1;

int y=++x;

 Prefix decrement operator decrements x value by 1 and assigns it to y.Now the value of x is 1 and y is 2.

 PostFix Decrement Operator :

int x=1;

Int y=x++;

From the above expression first value is assigned to y so the y value is 1 and then value gets decremented.Now the x value is 2.

After post decrement values are x=2,y=1.

 

295)    Explain complement operator (!) in java with example?

The complement operator is applied only to Boolean operands.It changes true value to false and false value to true.

boolean a=false;

boolean b=!a;

Now the value of a is false and value of b is true.