You are missing a field annotated with @Id
. Each @Entity
needs an @Id
- this is the primary key in the database.
If you don't want your entity to be persisted in a separate table, but rather be a part of other entities, you can use @Embeddable
instead of @Entity
.
If you want simply a data transfer object to hold some data from the hibernate entity, use no annotations on it whatsoever - leave it a simple pojo.
Update: In regards to SQL views, Hibernate docs write:
There is no difference between a view and a base table for a Hibernate mapping. This is transparent at the database level
For me, javax.persistence.Id
should be used instead of org.springframework.data.annotation.Id
. For anyone who encountered this issue, you can check if you imported the right Id
class.
This error can be thrown when you import a different library for @Id than Javax.persistance.Id ; You might need to pay attention this case too
In my case I had
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;
@Entity
public class Status {
@Id
@GeneratedValue
private int id;
when I change the code like this, it got worked
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.persistence.Id;
@Entity
public class Status {
@Id
@GeneratedValue
private int id;
The code below can solve the NullPointerException.
@Id
@GeneratedValue
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
If you add @Id
, then you can declare some more like as above declared method.
I think this issue following model class wrong import.
import org.springframework.data.annotation.Id;
Normally, it should be:
import javax.persistence.Id;
You are missing the @Id
entity property, and that's why Hibernate is throwing that exception.
Any JPA entity must have an identifier property, that is marked with the Id
annotation.
There are two types of identifiers:
An assigned identifier looks as follows:
@Id
private Long id;
Notice that we are using a wrapper (e.g.,
Long
,Integer
) instead of a primitive type (e.g.,long
,int
). Using a wrapper type is a better choice when using Hibernate because, by checking if theid
isnull
or not, Hibernate can better determine if an entity is transient (it does not have an associated table row) or detached (it has an associated table row, but it's not managed by the current Persistence Context).
The assigned identifier must be set manually by the application prior to calling persist:
Post post = new Post();
post.setId(1L);
entityManager.persist(post);
An auto-generated identifier requires the @GeneratedValue
annotation besides the @Id
:
@Id
@GeneratedValue
private int id;
There are 3 strategies Hibernate can use to auto-generate the entity identifier:
IDENTITY
SEQUENCE
TABLE
The IDENTITY
strategy is to be avoided if the underlying database supports sequences (e.g., Oracle, PostgreSQL, MariaDB since 10.3, SQL Server since 2012). The only major database that does not support sequences is MySQL.
The problem with
IDENTITY
is that automatic Hibernate batch inserts are disabled for this strategy.
The SEQUENCE
strategy is the best choice unless you are using MySQL. For the SEQUENCE
strategy, you also want to use the pooled
optimizer to reduce the number of database roundtrips when persisting multiple entities in the same Persistence Context.
The TABLE
generator is a terrible choice because it does not scale. For portability, you are better off using SEQUENCE
by default and switch to IDENTITY
for MySQL only.
Using @EmbeddableId for the PK entity has solved my issue.
@Entity
@Table(name="SAMPLE")
public class SampleEntity implements Serializable{
private static final long serialVersionUID = 1L;
@EmbeddedId
SampleEntityPK id;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With