Ebean enhancement - java

I'd like to create a small app, that uses Ebean as ORM. I've a little problem with the Ebean, and the enhancement, so I created a minimal example, that can be viewed here: https://github.com/vilmosnagy/ebean-example
It's a small Maven project, with one Entity, and one Dao. I wrote a test, where I'd like to save the entity into DB. (The DB is H2 in this example, 'cause to be small. I tried it with MySQL, but that didn't work either.)
When I run mvn clean install, I see the following lines in the output:
transform> pkg: com/example/entites
transform> cls: com/example/entites/BaseEntity msg: interfaces: entityInterface[false] transactional[false]
transform> cls: com/example/entites/BaseEntity msg: performing entity transform
transform> cls: com/example/entites/BaseEntity msg: ... adding intercept <init> in CONSTRUCTOR:()V OWNER/SUPER:java/lang/Object
transform> cls: com/example/entites/BaseEntity msg: adding equals() hashCode() and _ebean_getIdentity() with Id field id index:0 primative:false
transform> cls: com/example/entites/BaseEntity msg: enhanced
So, I think, my entity is enhanced succefully.
But when the tests run, I got the following exception:
Tests in error:
test_save(com.example.daos.BaseDaoTest): The type
[class com.example.entites.BaseEntity] is not a registered entity? If you don't
explicitly list the entity classes to use Ebean will search for them in the
classpath. If the entity is in a Jar check the ebean.search.jars property in
ebean.properties file or check ServerConfig.addJar().
I cannot find any solution. What am I missing?
Thanks!

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!

No Such Method error in HQL

I'm getting following error
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1173)
org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:248)
when execute the following hql
select object(o),p.dateApply,p.reason,p.Approver from Person as o "+
" com.jpa.entities.Application as p where o.id = p.nricNo and o.batchNumber= :batchNo
if i remove the following query
p.dateApply,p.reason,p.Approver
it's working just fine. The thing i want is to return value from both Person entities and Application entities.
Hope somebody can assist me as I'm new to HQL.
It sounds like you've got either incompatible Hibernate-related JARs, or duplicates of some or all of those JARs. Perhaps because you upgraded to a different version of Hibernate without deleting the old JARs (or something similar).
If different versions of the class com.my.MyClass are found in both lib1.jar and lib2.jar and the classloader loads MyClass from lib1.jar, but the "correct" MyClass (the one with the method the JRE is looking for) is found in lib2.jar, you'll experience exactly the problem you're seeing.

Hibernate Search complains about entity even if it is not indexed at all

I've recentlly added Hibernate Search to an existing project with existing entity classes and marked a few entities and fields with Indexed/Field.
However I get following exception at bootstrap time:
org.hibernate.search.SearchException: HSEARCH000135: Unable to guess FieldBridge for <some entity class which is not marked with #Indexed>
The class which is mentioned changes from time to time, but it is not a class which is marked with #Indexed nor it is related with an entity which is indexed.
Why does it even look at these classes?
I'm using Hibernate 4.2.8 with JPA2 and Hibernate Search 4.4.0.
If the entity in question has a composite ID, it is probably caused by this bug.

Hibernate reveng persistence model does not validate

I use Hibernate Reverse Engineering to automatically create classes from a database scheme. DB server is MSSQL 2008. This database is designed by a partner and could potentially change without notice. Thus I'd like to have Hibernate validate the scheme on startup, wich in my opinion should work out of the box. But it doesn't:
org.hibernate.HibernateException: Wrong column type in somedb.dbo.ASVC_S for column SomeCol. Found: decimal, expected: numeric(18,0)
The generated enttity class looks like this:
#Column(name="SomeCol", precision=18)
public BigDecimal getSomeCol() {
return this.someCol;
}
Is my assumption that reveng creates classes that can be validated against the schema wrong? Should I skip validation and hope that during runtime everything's OK? Annotating the classes after generating them or maintaining an entry for each class in my reveng.xml mapping file is not an option - too many classes ;)
hibernate-tools is version 4.0.0-CR1.

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

Categories

Resources