org.hibernate.annotations.Entity deprecated in Hibernate 4? - java

I am attempting to update to Hibernate 4 and I am getting that org.hibernate.annotations.Entity is deprecated. None of the documentation however seems to indicate that this is the case. Anyone have any insight into this?
#org.hibernate.annotations.Entity(dynamicUpdate = true)

Yes it is deprecated in 4.0+:
Deprecate org.hibernate.annotations.Entity
Its individual attributes/values should become annotations.
Schedule for removal in 4.1
You should use #DynamicUpdate instead
Here is a fixed JIRA talking about it.

From Hibernate Getting Started Guide :
The #javax.persistence.Entity annotation is used to mark a class as
an entity. It functions the same as the class mapping element
discussed in Section 2.3, "The mapping file". Additionally the
#javax.persistence.Table annotation explicitly specifies the table
name. Without this specification, the default table name would be
EVENT).
Since org.hibernate.annotations.Entity has been deprecated you should use the Java EE annotation. Also, as tolitius already mentioned, for the annotation configurations of #org.hibernate.annotations.Entity, you should use the respective annotation, e.g. #DynamicUpdate.
Hope that helps.
Note: Event is the name of the class that is annotated in the example, this is why it states "default table name would be EVENT".

Use the JPA #Entity annotation instead of the Hibernate #Entity annotation. Look in your imports, it should say
import javax.persistence.Entity;
and not
import org.hibernate.annotations.Entity;

For future purpose, please refer the deprecated API list for Hibernate 4.0. The link is as follows:-
Deprecated API

Related

How to avoid validation error "Cannot resolve column 'xy'" for embeddable classes in IntelliJ?

I'm using IntelliJ on a Java/Hibernate project. I've also assigned a data source to that project so most of the JPA validation errors for non existing columns are gone.
The only errors remaining are for these columns which are defined in #Embeddable classes like:
#Embeddable
public class MyEmbeddedClass {
#Column(name="my_embedded_column")
private String myEmbeddedColumn;
IntelliJ keeps warning me that these columns are not existing in the data model:
"Cannot resolve column 'my_embedded_column'"
Is there any way to make IntelliJ skip these JPA validation checks for #Embeddable classes without disabling the whole JPA validation functionality or am I supposed to create a bug ticket for the JPA validation plugin?
Same issue here. I've followd #Andrey suggestion and it worked for me.
I've moved my #embeddable classes into a subpackage (org.mydomain.back.entities.ids).
I've defined a scope, "MyCustomScope", exluding explicitly this subpackage. Then I've configured "Inspections-->JPA-->Unresolved databases references in annotations" like this:
Severity by Scope:
- MyCustomScope (allButEmbeddedKeys): Checked and "Error".
- Everywhere else: Unchecked.
It's more a workaround than an actual solution, because JPA inspection is disabled in such classes, with all inconveniences that could cause. But all errors in "embeddableId" classes are gone. Also for new classes defined into that subpackage.
Hope it helps!

The type Entity is deprecated in Eclipse [duplicate]

I am attempting to update to Hibernate 4 and I am getting that org.hibernate.annotations.Entity is deprecated. None of the documentation however seems to indicate that this is the case. Anyone have any insight into this?
#org.hibernate.annotations.Entity(dynamicUpdate = true)
Yes it is deprecated in 4.0+:
Deprecate org.hibernate.annotations.Entity
Its individual attributes/values should become annotations.
Schedule for removal in 4.1
You should use #DynamicUpdate instead
Here is a fixed JIRA talking about it.
From Hibernate Getting Started Guide :
The #javax.persistence.Entity annotation is used to mark a class as
an entity. It functions the same as the class mapping element
discussed in Section 2.3, "The mapping file". Additionally the
#javax.persistence.Table annotation explicitly specifies the table
name. Without this specification, the default table name would be
EVENT).
Since org.hibernate.annotations.Entity has been deprecated you should use the Java EE annotation. Also, as tolitius already mentioned, for the annotation configurations of #org.hibernate.annotations.Entity, you should use the respective annotation, e.g. #DynamicUpdate.
Hope that helps.
Note: Event is the name of the class that is annotated in the example, this is why it states "default table name would be EVENT".
Use the JPA #Entity annotation instead of the Hibernate #Entity annotation. Look in your imports, it should say
import javax.persistence.Entity;
and not
import org.hibernate.annotations.Entity;
For future purpose, please refer the deprecated API list for Hibernate 4.0. The link is as follows:-
Deprecated API

#Version is not being incremented in Spring mongo

I have added
#Enableauditng as an annotation but #version is not incrementing the version on its own.
I have #LastModifiedDate, which is being updated but not the #version.
Am i doing somehting wrong.
I am calling mongoTemplate.save(object) to againg and again.
Please advice
I think you are using:
javax.persistence.Version
Check your imports, #Version is part of javax.persistence (JPA) and has no relation to mongodb.
http://docs.spring.io/spring-data/mongodb/docs/1.5.0.RELEASE/reference/htmlsingle/
Unluckily, there is no hint to #Version in the spring data mongodb reference.
You should import org.springframework.data.annotation.Version for entity version information. See https://github.com/spring-projects/spring-data-mongodb/blob/1.5.x/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java
Make sure your "entity" class is a #org.springframework.data.mongodb.core.mapping.Document
and not a
#javax.persistence.Entity
Then, also make sure you're using:
#org.springframework.data.annotation.Id
and
#org.springframework.data.annotation.Version

org.hibernate.AnnotationException: #OneToOne or #ManyToOne on <entity> references an unknown entity

I am receiving the following Hibernate Exception:
org.hibernate.AnnotationException: #OneToOne or #ManyToOne on cz.rohan.dusps.model.Switchport.konfiguracniTemplateAccess references an unknown entity: cz.rohan.dusps.model.KonfiguracniTemplate
org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103)
org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)
org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
cz.rohan.dusps.helper.SessionFactoryHelper.initFactory(SessionFactoryHelper.java:122)
cz.rohan.dusps.helper.SessionFactoryHelper.getSessionFactory(SessionFactoryHelper.java:134)
cz.rohan.dusps.filter.HistorieZmenFilter.doFilter(HistorieZmenFilter.java:102)
cz.rohan.dusps.filter.CharsetFilter.doFilter(CharsetFilter.java:41)
after ~20 hours spent on the problem with various people, having read every possible blog or forum, I am really getting desperate here.
This is a mid-sized project. I should mention the database is Postgres 9.1 and we generate the DB using a modelling tool. Hibernate connects to the database but does not generate it.
I have created a new entity in the database, it's called "KonfiguracniTemplate" (configuration template). I have created the model, controller, form, validators, .jsp's, all basically copied 1:1 from an existing entity of a similar nature. I can now work with KonfiguracniTemplate, CRUD is fully working.
The problem comes when I reference this KonfiguracniTemplate from the entity called Switchport. In the DB there is a relation between the two:
Switchport 1:1 ... 0:N KonfiguracniTemplate (switchport always references a KonfiguracniTemplate; a KonfiguracniTemplate MAY BE referenced zero or more times)
Switchport has FK konfiguracniTemplateAccess_id for this relation.
In .../model/Switchport.java the relation is mapped just like all other relations that are working:
#ManyToOne
#JoinColumn(nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
I have tried various forms:
#ManyToOne
#JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
or
#ManyToOne(targetEntity=KonfiguracniTemplate.class)
#JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;
I have also checked:
both entities are in the same package
they are both annotated "#Entity" using "import javax.persistence.Entity;"
the build produces no error/warning messages
as long as the reference in Switchport is commented out, everything is fine
No matter what I try I cannot get rid of the "references an unknown entity" exception. Can somebody please share an idea what is happening or maybe how to debug the issue? The stacktrace at the top of the post is all I get in the logs.
All input is greatly appreciated!
Just add the class Team to the "hibernate-cfg.xml" file, because Hibernate doesn't identify without adding into it.
Possible Solutions:
1) Ensure that the entity has been appropriately referenced in hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
...
<mapping class="com.project.entitytwo.model.EntityTwo"/>
...
</session-factory>
2) Ensure that #Entity has been specified at the class-level ( at the top of the class )
#Entity
#Table( name="ENTITY_TWO" )
public class EntityTwo extends AnyClass
{
...
I just had this problem, with entity a referencing entity b. Both entities were located in a common JAR outside of the web project I was working on; a was declared in persistence.xml but b wasn't. I put b in a <class> tag in persistence.xml and it worked!
I ran into this problem when using Spring and not using the hibernate.cfg.xml file. It was solved by adding the fully qualified package name of the Model class to the setPackagesToScan method of LocalSessionFactoryBean class.
Finally got the solution from another developer on the team!
The classes need to be imported before the SessionFactory object is created. Here the import for the new class was missing, so it was unknown to the SessionFactory object.
Anyway, thanks everyone for your hints!
There is one more chance of getting such exception; when you don't mention your mapping class in hibernate.cfg.xml file.
As mentioned above.
I had same exception... I just forget add annotation (#Entity, and #Table) on MASTER class(class with Primary key)
so solution is double check every annotation in your entities , I mean not only #ManyToOne and #OneToMany like i did.
if your two entity in diffrent project,you can scan KonfiguracniTemplate's package in other project.you can do like this in spring boot
#EntityScan({"com.thispackage.entity","com.KonfiguracniTemplatepackage.entity"})
I'll give you a solution that should work for the same error with Spring Boot. This has less to do with the original question, but today, people would probably look for this answer instead because noone uses XML configuration today anymore.
I suffered the same problem and found the solution on this website: https://www.programmersought.com/article/1617314625/
He even describes this very question he would have looked up, but then I'm asking myself: why didn't he answered here after finding the solution? LOL
His own words:
In the Spring Boot project, the default scan package is the package where the main method is located, that is, only the entity classes in the same package as the main method will be discovered. This way you can understand why User is not found: because User is an entity class in another module. Spring Boot does not scan other packages at all;
Configure the #SpringBootApplication annotation on the main method that launches the application, telling Spring Boot that those packages need to be scanned: #SpringBootApplication(scanBasePackages = {“com.xiaomo.*”})
Then, User can be found.
So you basically reconfigure SpringBoot to scan more packages to include the other ones.
My personal addition: you could also move your packages into the package where the starter is located or move the starter a package up (that's what I did).

Is is possible to create a custom jpa annotation in java

Jpa one of the big successfull module of jpa and so are its annotation features .I have weird requirement in which i need to create jpa annotation ,one that jpa can process
ex. We have in jpa a table annotation that create a table for this java pojo class.
i need to make another annotation that behaves exactly as what table annotation does + some of my custom reqirements;
what is mean
if i create a customAnnotation like #Anil that is suppose to work same as #Table Annotation does
than
#Anil
public class Anp
{
}
than this should create a table in the database is that possible or not .if it is give me some way to do this
thanks
JPA does not process just any annotation. The JPA implementation processes the annotations that it supports and these are typically just the javax.persistence annotations, and optionally its own extensions. Your JPA provider may allow you to define your own, but this is not going to be very common - look at the docs for your JPA provider if they allow a user to define annotations.
For example, the JPA provider I have used (DataNucleus JPA) allows the user to provide annotations for the class or for the field/property.

Categories

Resources