I'm working on a project in java with a postgresql database and I'm having a problem when I try to insert data or rather when I commit. It looks like it comes from a problem with ID (game_id) but I do not know what.
Here is the code of my entity :
@Entity
@Cacheable(true)
@Table(name = "game")
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
public class Game implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "game_id", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer gameId;
@Column(name = "game_title", length = 256)
private String gameTitle;
@Temporal(TemporalType.DATE)
@Column(name = "game_released")
private Date gameReleased;
@Column(name = "game_img")
private Byte[] gameImg;
@Column(name = "game_desc", length = 3072)
private String gameDesc;
And here's how I try to insert my data :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("projet_nintendo");
EntityManager em = emf.createEntityManager();
EntityTransaction transac = em.getTransaction();
transac.begin();
for(int ii = 0; ii < array.length(); ii++) {
Game g = new Game();
g.setGameId(Conversion.stringEnInt(array.getJSONObject(ii).getString("fs_id")));
g.setGameTitle(array.getJSONObject(ii).getString("title"));
JSONArray test = array.getJSONObject(ii).getJSONArray("dates_released_dts");
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date parsedDate = dateFormat.parse(test.getString(0));
g.setGameReleased(parsedDate);
} catch(Exception e) {
}
em.persist(g);
System.err.println(array.getJSONObject(ii).getString("pretty_date_s"));
}
transac.commit();
em.close();
emf.close();
I have this error :
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-69] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A NullPointerException was thrown while extracting a value from the instance variable [gameId] in the object [database.orm.Game].
Internal Exception: java.lang.NullPointerException
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[gameId-->game.game_id]
Descriptor: RelationalDescriptor(database.orm.Game --> [DatabaseTable(game)])
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
at test.main(test.java:79)
Caused by: Exception [EclipseLink-69] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd)
Could you tell me what I'm doing wrong?