These are chat archives for hibernate/hibernate-orm

23rd
Aug 2016
Miguel Almeida
@mmalmeida
Aug 23 2016 16:51
@Naros do you have any idea to get it started?
ryan2049
@ryan2049
Aug 23 2016 16:52
Check his SO response
Miguel Almeida
@mmalmeida
Aug 23 2016 16:52
@ryan2049 his SO response is based on JPA criteria
just FYI, the reason for using criteria is that I'm using this for an UI table filter, so with variable parameters. and a addFiltersToCriteria(Criteria criteria, Filter codeFilter) is responsible for iterating the Filter and, based on the parameters it has, dynamically build the criteria by adding restrictions to it
Miguel Almeida
@mmalmeida
Aug 23 2016 16:57
@ryan2049 - @Naros mentioned doing it with Hibernate Criteria API. I reckon that link is for JPA criteria
ryan2049
@ryan2049
Aug 23 2016 17:00
Naros' response on SO is JPA criteria
Miguel Almeida
@mmalmeida
Aug 23 2016 17:03
@ryan2049 I am interested in Hibernate Criteria. Naros replied with JPA, and said it probably wasn't possible with Hibernate Criteria. then yesterday he said " you could probably do it with subquery with Hibernate Criteria API"
and I was asking him if he had tried it out, since he probably didn't have the time to update the SO reply accordingly
ryan2049
@ryan2049
Aug 23 2016 17:04
Does hibernate criteria have a MapJoin equivalent? I can't remember
Hibernate criteria is deprecated afaik
Miguel Almeida
@mmalmeida
Aug 23 2016 17:05
I don't think it does :/
Chris Cranford
@Naros
Aug 23 2016 17:05
@mmalmeida I won't be able to do much until Saturday at the earliest. I'm on PTO and moving :)
I'd look to see though if you could use a subquery to get what you want
Not 100% sure if possible with Hibernate Criteira, just a thought to look into since you can't use JPA
Miguel Almeida
@mmalmeida
Aug 23 2016 17:08
@ryan2049 - yeah, I only realised it was deprecated recently. and unfortunately it means re-writing all the hooks to the db since you can't use sessionFactory. current applications we have have a nice set-up, with spring configuring the sessionFactory, setting the Txmanagement...haven't discovered the work involved in upgrading to EntityManager yet
(and consequently I don't know if it's worth the trouble)
Chris Cranford
@Naros
Aug 23 2016 17:09
With 5.2+ it should be closer to a complete replacement.
e.g. change SessionFactory to EntityManagerFactory
e.g change Session to EntityManager
Miguel Almeida
@mmalmeida
Aug 23 2016 17:10
yeah, it doesn't look very complicated. it's spring's management of it that I don't think is so straightforward. I've looked at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html
Chris Cranford
@Naros
Aug 23 2016 17:10
personally, I like the Spring JPA integration, just inject a @PersistenceContext private EntityManager entityManager into your bean and use it.
you do all the wiring in the configuration or xml of spring, done.
Most of my dao beans in the past mirrored that of the ProductDaoImpl near the bottom of that page link you sent Miguel.
right before transaction mgmt.
That way spring manages giving you an EM, you just use it.
Miguel Almeida
@mmalmeida
Aug 23 2016 17:14
while the documentation is clear for the sf, I didn't dig in to see what I need to change (e.g. is there an equivalent to dataSource to set the connection pooler, hibernateProperties to set the database and credentials, annotatedPackages and mappingLocations to define the entities' locations?)
namingStrategy, hibernate interceptors...
Chris Cranford
@Naros
Aug 23 2016 17:15
You basically provide a Map of Key/value pairs to your the entity manager factory bean that get past onto the EMF and EM inside Hibernate. You'll have to look to see if 1) the EMFB exposes a setter for it and if so, use that or 2) if no setter, use the appropriately named key to pass it in the map
That's one of the EMFB impls i think spring gives you that you can use
LocalSessionFactoryBean for example -> LocalEntityManagerFactory in configuration :)
Miguel Almeida
@mmalmeida
Aug 23 2016 17:17
yeah, it doesn't look too complicated
Chris Cranford
@Naros
Aug 23 2016 17:18
nope, you'll probably find a fair bit of overlap tbh with what you did with a SF vs EMF stuffs
Miguel Almeida
@mmalmeida
Aug 23 2016 17:18
(anyway, not something you'd do on the week of the release :p)
Chris Cranford
@Naros
Aug 23 2016 17:19
come on, where's the adventure!!!!!! :P
Miguel Almeida
@mmalmeida
Aug 23 2016 17:19
hahaha :D
Chris Cranford
@Naros
Aug 23 2016 17:19
with a SF you may just have to live with a slight change in data model until you migrate to EM
tho, hmm
I wonder if you could get by using JPQL
or even HQL
Miguel Almeida
@mmalmeida
Aug 23 2016 17:20
I am considering HQL to get it
ryan2049
@ryan2049
Aug 23 2016 17:20
@mmalmeida have you tried to delve into the properties map with the createCriteria() method?
Chris Cranford
@Naros
Aug 23 2016 17:20
yah that is probably the best short-term fix imo
ryan2049
@ryan2049
Aug 23 2016 17:21
ie. treat it as a regular entity
and see what happens
Chris Cranford
@Naros
Aug 23 2016 17:21
anywho, afk for a bit.
Miguel Almeida
@mmalmeida
Aug 23 2016 17:21
I was trying to keep with criteria because it's simpler for this scenario where you dynamically add restrictions (criteria.add vs queryString + = " and ...")
ryan2049
@ryan2049
Aug 23 2016 17:22
yeah that's what i'm saying
Miguel Almeida
@mmalmeida
Aug 23 2016 17:24
@ryan2049 do you mean .createCriteria(Code.class, "code").createCriteria(X, "propertiesMap") ?
ryan2049
@ryan2049
Aug 23 2016 17:25
Criteria criteria = currentSession()
    .createCriteria(Code.class, "code")
    .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
Criteria subCriteria = criteria.createCriteria("properties");
subCriteria.add(Restrictions.eq("foo", "test1");
criteria.list();
i typed that slightly wrong
but you can create child criteria objects off of the original criteria and then add your restrictions against that object
Miguel Almeida
@mmalmeida
Aug 23 2016 17:26
let me try
ryan2049
@ryan2049
Aug 23 2016 17:26
you may have to tweak what I wrote to get it to match up properly
my guess is that it will complain that you can't create a path to properties because it's not an entity, but it's worth a try
I wrote a wrapper implementation to Hibernate Criteria in 2011ish, but it's been so long and I don't remember all the idiosyncrasies
Miguel Almeida
@mmalmeida
Aug 23 2016 17:30
when I add the subCriteria it complains: org.hibernate.QueryException: could not resolve property: foo of: com.example.CodeValue
ryan2049
@ryan2049
Aug 23 2016 17:30
I don't know if foo is a valid property
is it?
Miguel Almeida
@mmalmeida
Aug 23 2016 17:30
ie, it treats the subCriteria entity as a CodeValue
ryan2049
@ryan2049
Aug 23 2016 17:30
do you have this code uploaded to github or something?
Miguel Almeida
@mmalmeida
Aug 23 2016 17:31
unfortunately not :/
ryan2049
@ryan2049
Aug 23 2016 17:32
I would recommend setting up a test case and uploading it to github so that we could clone it and try to come up with a solution that would suit your needs
Miguel Almeida
@mmalmeida
Aug 23 2016 17:33
will do that :)
ryan2049
@ryan2049
Aug 23 2016 17:34
Hibernate Criteria is deprecated, but it would give us a better avenue of coming up with a workaround
thanks
Miguel Almeida
@mmalmeida
Aug 23 2016 17:46
I was trying the detachedCriteria way, but I got stuck in the same problem. XXX in the query below - I want to say "the map key is in dc", but that's the whole problem
                DetachedCriteria dc = DetachedCriteria.forClass(CodeProperty.class);
                dc.add(Restrictions.ilike("attribute", propertyName));
                dc.setProjection(Property.forName("id"));

                criteria.add(Property.forName(XXX).in(dc));
since, as mentioned in the SO, create alias to properties will make it a CodeValue, not a CodeProperty