Tag Archives: jpa

JPA GenerationType.AUTO not considering column with auto increment

Questions: I have a table with a simple int id column with Identity auto increment in SQL Server. The entity’s Id is annotated with @Id and @GeneratedValue @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = “id”, length = 4, precision = 10, nullable = false) private Integer id; In SQL Server the column is properly set as… Read More »

Hibernate (/JPA) server-side paging and MS SQL Server

Questions: I implementing a server-side data paging with hibernate / JPA, the underlying database is MS SQL Server 2008. The SQL is generated like this: criteria.setFirstResult(pagingParams.getDisplayStart()) .setMaxResults(pagingParams.getDisplayLength()); (The main work lays in creating the appropriate filter / sorting, but that’s not relevant here) What I’m observing is the following SQL: page (0-20): select top 20… Read More »

JPA getResultList() returns BigInteger for MySQL but Integer for Microsoft SQL server

Questions: I have following method: Query q = getEntityManager().createNativeQuery(“SELECT COUNT(1) FROM table1 WHERE column = :column_id ” + “UNION ” + “SELECT COUNT(1) FROM table2 WHERE column = :column_id”); q.setParameter(“column_id”, column_id); When I want to get the list of counts (which will be 2 rows), I perform this action: List<BigInteger> counts = (List<BigInteger>) q.getResultList(); This… Read More »

JPA composite primary key [duplicate]

Questions: This question already has an answer here: Mapping ManyToMany with composite Primary key and Annotation: 2 answers Answers: Recently I created ManyToMany relation using Composite Primary key and annotation as bi directional @OneToMany. This code works flawless. Maybe it will help: Mapping ManyToMany with composite Primary key and Annotation: Questions: Answers: Since you are… Read More »

Java – JPA – Generators – @SequenceGenerator

Questions: I am learning JPA and have confusion in the @SequenceGenerator annotation. To my understanding, it automatically assigns a value to numeric identity fields/properties of an entity. Q1. Does this sequence generator make use of the database’s increasing numeric value generating capability or generates the number on it’s own? Q2. If JPA uses database auto… Read More »

Disable caching in JPA (eclipselink)

Questions: I want to use JPA (eclipselink) to get data from my database. The database is changed by a number of other sources and I therefore want to go back to the database for every find I execute. I have read a number of posts on disabling the cache but this does not seem to… Read More »

JPA getSingleResult() or null

Questions: I have an insertOrUpdate method which inserts an Entity when it doesn’t exist or update it if it does. To enable this, I have to findByIdAndForeignKey, if it returned null insert if not then update. The problem is how do I check if it exists? So I tried getSingleResult. But it throws an exception… Read More »

Specifying an Index (Non-Unique Key) Using JPA

Questions: How do you define a field, eg email as having an index using JPA annotations. We need a non-unique key on email because there are literally millions of queries on this field per day, and its a bit slow without the key. @Entity @Table(name=”person”, uniqueConstraints=@UniqueConstraint(columnNames={“code”, “uid”})) public class Person { // Unique on code… Read More »

JPA fastest way to ignore a field during persistence?

Questions: I’m essentially looking for a “@Ignore” type annotation with which I can stop a particular field from being persisted. How can this be achieved? Answers: @Transient complies with your needs. Questions: Answers: To ignore a field, annotate it with @Transient so it will not be mapped by hibernate. but then jackson will not serialize… Read More »