Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Chris Cranford
@Naros
@easonzhang1992 You can write a JPQL/HQL query, a JPA Criteria Query, or a Native SQL query updating a row using any number of column predicates.
I believe the only time Hibernate will mandate using the id values would be if you run entityManager.merge( yourEntity ); as you naturally are wanting to update that specific instance/row.
In the merge case, I don't understand why you'd want to leverage a different set of fields since that logically doesn't make sense to me.
MOHAMD DORRA
@mhddurrah
Hello, I wanted to ask about @where annotation
when applying it over a collection (OnToMany) it works fine
but when applying it to OneToOne It doesn't work
is there any reason
@Where and @SQLDelete in concept seem simple but there are some unexpected behaviors applying them
let me generalize the question
did any one use @Where and @SQLDelete for complex scenarios?
is there any official documentation for that
Steve Ebersole
@sebersole
@mhddurrah
/**
 * Where clause to add to the element Entity or target entity of a collection.  The clause is written in SQL.
 * A common use case here is for soft-deletes.
 */
public @interface Where {
....
Added to (1) an entity or (2) collection
Chris Cranford
@Naros
When we mean entity, we mean something like this:
@Entity
@Where( ... )
public class SomeSpecialEntity {
 ...
}
In your second image, you're applying the annotation on an association, not the entity itself.
Hopefully that clears up the confusion.
MOHAMD DORRA
@mhddurrah
@sebersole I guess there a change in versions, It is working for the collection but not for OneToOne child entity
this is my setup
https://prntscr.com/o4jaz7
https://prntscr.com/o4jaw3
and these are the test results
https://prntscr.com/o4jbl7
https://prntscr.com/o4jb9b
hibernate version: 5.2.10.Final
@Naros annotating works for query's top entities but not joined one
Chris Cranford
@Naros
So what you are saying is you have an entity A that has a xxxx-to-one association to entity B; you query entity As and the @Where you have on the entity B mapping doesn't get applied on that query?

e.g.

@Entity
public class EntityA {
  @ManyToOne
  private EntityB b;
}

@Entity
@Where(...)
public class EntityB {
  ...
}

and your query is something like SELECT a FROM EntityA a WHERE ... and you dont see the @Where be applied when it does the implicit join to EntityB.

easonzhang1992
@easonzhang1992
@Naros thank you very much. as you said. i resovled the problem. i put the @id on the field and itworks thanks again!
Steve Ebersole
@sebersole
@mhddurrah there is no "change in versions" @Where associated with an entity is not included in to-one associations
thats always been that way
nor is it going to change (before you ask)
MOHAMD DORRA
@mhddurrah
@Naros I applied the annotation on both child's entity class and parent entity's member but I got the same result
@sebersole :) that is because I read something about applying Where and SQLDelete
MOHAMD DORRA
@mhddurrah
@sebersole So is it a limitation or is it an intended behavior
if it is an intended behavior give us a reference or clarify why we don't need to exclude the soft deleted entities from the one to one relation?
MOHAMD DORRA
@mhddurrah
@sebersole @Naros any feedback? thanks
Steve Ebersole
@sebersole
you are looking at it backwards. I mean based on that logic I could expect to combine Chocolate and Vanilla just because nowwhere does it say I can't
so I'll turn it back around to you from the correct perspective... where did you see that these 2 should work together?
MOHAMD DORRA
@mhddurrah
Let us ignore the collection case, if I used @Where annotation on a OneToOne relation, should/would it work or not? this is because of expectation as a developer from that behaviour. Thanks for replying @sebersole , I appreciate that
Vincenzo Palazzo
@vincenzopalazzo
Hi guys I have a problem with mapping a Three structure int particular, I have an element father with twoo children and the children have the same father but the mapping is @OneToOne and hibernate generater this exception aused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 32769, for class:. my question is, exist a method for mapping this case or I must change the stuctore and add an list oh the children?
@Entity(name = "abstract_node")
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractNode implements INode, Comparable<AbstractNode> {

private static final Log LOGGER = LogFactory.getLog(AbstractNode.class);

protected String value;
protected int frequency;
protected int valueArch;                         
@OneToOne(mappedBy = "father", cascade = CascadeType.ALL) private AbstractNode leaftNode;
@OneToOne(mappedBy = "father", cascade = CascadeType.ALL)private AbstractNode rightNode;

//for hibernate 
private Long id;
@OneToOne private MessageUser message;
@OneToOne private AbstractNode father;

public AbstractNode(AbstractNode leaftNode, AbstractNode rightNode) {

    this.leaftNode = leaftNode;
    this.rightNode = rightNode;

    this.leaftNode.setFather(this);
    this.leaftNode.setValueArch(0);

    this.rightNode.setFather(this);
    this.rightNode.setValueArch(1);

    if (this.leaftNode.getFather() == null) {
        throw new IllegalArgumentException("Fater null");
    }

    if (this.rightNode.getFather() == null) {
        throw new IllegalArgumentException("Fater null");
    }
}  //getter and setter
Chris Cranford
@Naros
@mhddurrah What Steve is trying to get at is more the why do you think or where do you become informed that a one-to-one should behave like a collection or an entity mapping?
A @OneToOne to an entity isn't the same thing as an entity mapping, its an association mapping that points to an entity. In the world of ORM those are two different concepts all together.
Vincenzo Palazzo
@vincenzopalazzo
Hi @Naros you mean for my post? I think no
Chris Cranford
@Naros
@vincenzopalazzo No sorry, my response was for @mhddurrah
Vincenzo Palazzo
@vincenzopalazzo
no problem I asked you for I want not make ugly figures with you haha
prog20901
@prog20901

I have two tables

Employee Table
Address Table
Employee Table hbm definition will have the following

<id name="empGkey" column="primarykey" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>

<many-to-one name="empAddress" column="address" class="com.region.Address" cascade="all" unique="false"> </many-to-one>
Address Table hbm definition will have the following

<id name="addressGkey" column="primarykey" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<one-to-one name="addressEmp" class="com.region.Employee" property-ref="empAddress">
</one-to-one>
Currently the application throws the following exception when execute the following HQL query

from Address where primarykey = '1232';

More than one row with the given identifier was found: 1232, for class:
com.region.Employee (compact stack trace follows)
org.hibernate.loader.entity.AbstractEntityLoader.load
(AbstractEntityLoader.java:92)
Clarification

  1. Will Address primary key will always have one similar primary key in
    Employee?
  2. Will the Address primary key might differs from Employee primary key
    column
  3. Will the Address primary key will always have one similar address column
    value in Employee table?
  4. When this issue might come?
  5. How to fix the problem
    Thanks.
Steve Ebersole
@sebersole
@vincenzopalazzo well you define that 2 different values (leaftNode and rightNode) should each share PK value with its owner ("father"). So assuming a "father" has both a left and a right you end up with 2 AbstractNode references (one for left and one for right) with the same PK - the one they share with "father"
Vincenzo Palazzo
@vincenzopalazzo
Hi @sebersole Thanks for your answare, so I define the id fater with JoinColum right?
cthlo
@cthlo
Hi here, how do I disable caching on one-to-one association?

Entity Food:

<class name="Food">
    <one-to-one name="foodMetadata" lazy="proxy" constrained="true" fetch="select" />
</class>

Entity FoodMeta:

<class name="FoodMetadata">
    <subselect>
         <!-- some complex query that depends on `customer` -->
    </subselect>
    <synchronize table="customer"/>
    <id name="id"/>
    <property ... />
</class>
For some reason, updating customer does not invalidate the cache on FoodMetadata.
cthlo
@cthlo
I also have batch-size="100" in FoodMeta
Looks like synchronize is working if batch-size is removed.
cthlo
@cthlo
How do I refresh a PersistentMap (i.e. <map name="..."> ... </map>) property?
oğuz karakuş
@karakus_oguz_twitter
Hi All,
when I set 'javax.persistence.query.timeout' hint to entity manager for query time outs, it seems that it is not working with miliseconds but instead it rounds it to seconds.
Also see here, it is set to 500ms but comments says 1 sec. https://github.com/hibernate/hibernate-orm/blob/800b60e648b5696e00d006e70f8e0fa2c296fcc0/hibernate-core/src/test/java/org/hibernate/jpa/test/lock/LockTest.java#L944
Is there any work around for this? I want to be able to set time outs less then 1 sec.
Steve Ebersole
@sebersole
@karakus_oguz_twitter What does "but comments says 1 sec" mean?
also, considering you are locking... are you sure you do not want javax.persistence.lock.timeout?
different things
Kevin Madhu
@kevin-madhu

Hi everyone

I need to have a field, which is an auto_increment field, but with a category
For example, I have one entity named 'Field' with two attributes 'name' and 'code'.
If I have 'field1' and 'field2' for example with codes 'code1' and 'code2 respectively.
If field1 is selected initially, each insert will produce values, 'code1_1', 'code1_2' etc and then I switch to 'field2'

Now the next insert will produce value 'code2_1'
Is this possible with SequenceStyleGenerator or something?

https://thoughts-on-java.org/custom-sequence-based-idgenerator/
Section - Year and month followed by a sequence-based value
Relevant text -
A common scenario for this kind of id generator requires you to reset the sequence number at the end of each day or month or year. This can’t be handled by the id generator. You need to configure a job that resets your database sequence in the required intervals and the DatePrefixedSequenceIdGenerator will automatically get the new values when it requests the next value from the sequence.

But, I don't know how to write such a job.

Thanks in advance :)

wencheng
@wencheng1994
Hello, I have some trouble using hibernate: https://gist.github.com/wencheng1994/ed86eb56f90617517ee679463c6ac064
Does any one konw this issue?
wencheng
@wencheng1994
I solve that. It mainly because the db account has a higher authority. I set the "hibernate.hbm2ddl.auto=update", So when hbm2ddl works, it tried to find all exists shcema I defied. But there is two schema exist the table with the same name. then the db account can find that. so it found "more than one table in the namespace"
Giovanni Lovato
@heruan
Anybody with experience on Java 9+ modules and Hibernate? When in module mode, org.hibernate.Version.getVersionString() returns [WORKING] instead of the Hibernate version.
Steve Ebersole
@sebersole
Internally it uses java.lang.Package#getImplementationVersion which apparently does not work with Java 9 modules
Giovanni Lovato
@heruan
Yeah I tested the same project removing the module-info and it works, so it’s definitely that. This causes external libraries relying on Version.getVersionString() to fail parsing (e.g. Blaze Persistence).