我正在用Postgresql数据库在Java中的项目上工作,当我尝试插入数据或提交时遇到问题。 看来是由于ID(game_id)问题引起的,但我不知道是什么。
这是我实体的代码:
@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;
这是我尝试插入数据的方法:
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();
我遇到此错误:
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)
你能告诉我我在做什么错吗?
答案 0 :(得分:0)
try(EntityManager em = emf.createEntityManager();
EntityTransaction transac = em.getTransaction()){
...
}
// don't need to close here em.. emf..
答案 1 :(得分:0)
尝试删除您的@GeneratedValue
。当前您正在使用
GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
这将导致数据库自动为您的id字段分配一个新值,然后EclipseLink必须检索它。