[JIRA] (HHH-16216) @Query creates additional not null checks for Sybase
by Marius K (JIRA)
Marius K ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNTRkYzczMTcy... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16216?atlOrigin=eyJpIjoiNTRkYz... ) HHH-16216 ( https://hibernate.atlassian.net/browse/HHH-16216?atlOrigin=eyJpIjoiNTRkYz... ) @Query creates additional not null checks for Sybase ( https://hibernate.atlassian.net/browse/HHH-16216?atlOrigin=eyJpIjoiNTRkYz... )
Issue Type: Bug Affects Versions: 6.2.0.CR2, 6.1.7 Assignee: Unassigned Components: query-hql Created: 22/Feb/2023 01:00 AM Priority: Critical Reporter: Marius K ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... )
I upgraded from Spring Boot 2.7 to 3.0, hence from Hibernate 5 to Hibernate 6
Unfortunately the @Query annotations in a JpaRepository suddenly add not null checks for all columns using Sybase.
It works fine with h2
@Entity
@Table(name = "MY_USER")
@Getter
@Setter
@EqualsAndHashCode
public class MyUser {
@Id
@Column(name = "MY_USER_ID", unique = true, nullable = false)
private Integer myUserId;
@Column(name = "FIRSTNAME")
private String firstname;
@Column(name = "LASTNAME")
private boolean lastname;
}
and
public interface MyUserRepository extends JpaRepository<MyUser, Integer> {
@Query("select r from MyUser r where r.firstname = :firstname and r.lastname = :lastname")
List<MyUser> getUser(@Param("firstname") String firstname, @Param("lastname") String lastname);
}
results in the query
select m1_0.my_user_id,m1_0.firstname,m1_0.lastname from my_user m1_0 where m1_0.firstname=? and m1_0.lastname=?
which is obviously fine.
But when I switch to Sybase
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.sybase.jdbc4.jdbc.SybDriver
spring.jpa.database-platform=org.hibernate.dialect.SybaseASEDialect
<dependency>
<groupId>com.sybase</groupId>
<artifactId>jconn4</artifactId>
</dependency>
the query is
select m1_0.my_user_id,m1_0.firstname,m1_0.lastname from my_user m1_0 where m1_0.firstname=? and m1_0.firstname is not null and m1_0.lastname=? and m1_0.lastname is not null
with additional null checks. This breaks my code as it is no longer possible to pass null to the parameters in the getUser ` method.
( https://hibernate.atlassian.net/browse/HHH-16216#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16216#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100216- sha1:64a4cf6 )
1 year, 10 months
[JIRA] (HHH-16215) Composite primary key @IdClass attribute mapping is borrowed from the first OneToMany backref and cannot be set
by Alina Ricciuti (JIRA)
Alina Ricciuti ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61fb92a... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYzYzZDdiZTAz... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16215?atlOrigin=eyJpIjoiYzYzZD... ) HHH-16215 ( https://hibernate.atlassian.net/browse/HHH-16215?atlOrigin=eyJpIjoiYzYzZD... ) Composite primary key @IdClass attribute mapping is borrowed from the first OneToMany backref and cannot be set ( https://hibernate.atlassian.net/browse/HHH-16215?atlOrigin=eyJpIjoiYzYzZD... )
Issue Type: Bug Assignee: Unassigned Created: 21/Feb/2023 14:27 PM Environment: Hibernate: 6.1.7.Final, 6.2.0.CR2
Postgresql: 11
Spring Data JPA: 3.0.1
Spring Boot: 3.0.2
JDK: Oracle OpenJDK 17.0.2
OS: Windows 11 Priority: Major Reporter: Alina Ricciuti ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61fb92a... )
Please find below the issue I have encountered during the migration from hibernate 5 to hibernante 6.1.7.
My sample project is using Lombok / Spring Data; its datamodel is composed of :
* Buckets having Lines
@Getter
@Setter
@Entity
@Table(name = "BUCKET")
@SequenceGenerator(name = "SEQ_BUCKET_ID", allocationSize = 10, sequenceName = "SEQ_BUCKET_ID")
@IdClass(PkBucket.class)
public class Bucket {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_BUCKET_ID")
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "BUCKET_ID", referencedColumnName = "id", nullable = false)
private List<Line> lines = new ArrayList<>();
}
* Items having Lines
@Getter
@Setter
@Entity
@Table(name = "LINE")
@SequenceGenerator(name = "SEQ_LINE_ID", allocationSize = 10, sequenceName = "SEQ_LINE_ID")
@IdClass(PkLine.class)
public class Line {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LINE_ID")
private Long id;
@ManyToOne(targetEntity = Item.class)
@JoinColumn(name = "ITEM_ID", referencedColumnName = "id", nullable = false)
private Item item;
}
* The Line class
@Getter
@Setter
@Entity
@Table(name = "ITEM")
@SequenceGenerator(name = "SEQ_ITEM_ID", allocationSize = 10, sequenceName = "SEQ_ITEM_ID")
@IdClass(PkItem.class)
public class Item {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ITEM_ID")
private Long id;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Line> lines = new ArrayList<>();
}
* The simplified Pk classes - they are all identical, as PkBucket here below
@Getter
@Setter
public class PkBucket implements Serializable {
private Long id;
public PkBucket withId(Long id) {
this.id = id;
return this;
}
}
My test is creating and updating some data :
@Test
@Transactional
public void buckets_items_lines() {
// create some data
Session session = entityManager.unwrap(Session.class);
session.createNativeQuery("insert into ITEM (id) values (100)").executeUpdate();
session.createNativeQuery("insert into ITEM (id) values (101)").executeUpdate();
session.createNativeQuery("insert into BUCKET (id) values (200)").executeUpdate();
session.createNativeQuery("insert into LINE (id, ITEM_ID, BUCKET_ID) values (300, 100, 200)").executeUpdate();
// move line from item to other item
Item item100 = itemRepository.findById(new PkItem().withId(100L)).get();
final Item item101 = itemRepository.findById(new PkItem().withId(101L)).get();
item100.getLines().forEach(line -> line.setItem(item101));
}
The issue below occurs when tying to hydrate Item.lines:
Could not set value of type [java.lang.Long] : `com.example.demo.domain.issue2.PkBucket.id` (setter)
org.hibernate.PropertyAccessException: Could not set value of type [java.lang.Long] : `com.example.demo.domain.issue2.PkBucket.id` (setter)
at app//org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:81)
at app//org.hibernate.metamodel.mapping.internal.AbstractEmbeddableMapping.setValues(AbstractEmbeddableMapping.java:103)
Few remarks:
* As you can see in the stack attached, the Line property id is accessed through PkBucket id instead of PkLine id.
* If I remove nullable = false from Bucket lines JoinColums the result is OK
* If I use unique Pk class the issue persists : IllegalArgumentException: Can not set java.lang.Long field com.example.demo.domain.issue2.Pk.id ( http://com.example.demo.domain.issue2.Pk.id ) to com.example.demo.domain.issue2.Line
* If I force the Line class to extend the expected id class the result is OK but this is not nice 🙂
...
@IdClass(PkLine.class)
public class Line extends PkBucket {
...
I have attached the full log; if needed, I can also share my sample project.
( https://hibernate.atlassian.net/browse/HHH-16215#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16215#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100216- sha1:64a4cf6 )
1 year, 10 months