Hibernate,H2在建立实体管理器(Eclipse/Java)时出错



下面是我运行分配给我的测试类时收到的错误:

2019-11-14T19:58:38.309Z [34mINFO [0;39m [main      ] [36mc.a.cst8277.assignment3.Test[0;39m - setup JPA EntityManagerFactory, create EntityManager (Session)
2019-11-14T19:58:40.004Z [34mINFO [0;39m [main      ] [36meclipselink.logging.all[0;39m - EclipseLink, version: Eclipse Persistence Services - 2.7.3.v20180807-4be1041
2019-11-14T19:58:40.021Z [34mINFO [0;39m [main      ] [36meclipselink.logging.connection[0;39m - connecting(DatabaseLogin(
platform=>H2Platform
user name=> "sa"
datasource URL=> "jdbc:h2:mem:Assignment3"
))
2019-11-14T19:58:40.429Z [34mINFO [0;39m [main      ] [36meclipselink.logging.connection[0;39m - Connected: jdbc:h2:mem:Assignment3
User: SA
Database: H2  Version: 1.4.199 (2019-03-13)
Driver: H2 JDBC Driver  Version: 1.4.199 (2019-03-13)
2019-11-14T19:58:40.512Z [1;31mERROR[0;39m [main      ] [36meclipselink.logging.all[0;39m - Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------
Exception [EclipseLink-46] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the primary key field [PORTFOLIO.PORTFOLIO_ID].
Descriptor: RelationalDescriptor(com.algonquincollege.cst8277.assignment3.model.Portfolio --> [DatabaseTable(PORTFOLIO)])

最后一部分对所有类都重复,我还得到了另一组错误,这些错误也对我创建的所有类重复:

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(com.algonquincollege.cst8277.assignment3.model.Portfolio --> [DatabaseTable(PORTFOLIO)])

这是我需要运行的测试类:

/**************************************************************G*********o****o****g**o****og**joob*********************
* File: Test.java
* Course materials (19F) CST 8277
* @author Mike Norman
*
* @date 2019 10
*/
package com.algonquincollege.cst8277.assignment3;
import java.lang.invoke.MethodHandles;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.algonquincollege.cst8277.assignment3.dao.BankUserDAOImpl;
public class Test {
private static final Class<?> _thisClaz = MethodHandles.lookup().lookupClass();
private static final Logger logger = LoggerFactory.getLogger(_thisClaz);
public static final String ASSIGNMENT3_PU_NAME = "Assignment3-main-PU";
public static void main(String[] args) {
logger.info("setup JPA EntityManagerFactory, create EntityManager (Session)");
EntityManagerFactory emf = Persistence.createEntityManagerFactory(ASSIGNMENT3_PU_NAME);
EntityManager em = emf.createEntityManager();
em.close();
emf.close();
}
}

这是有问题的8个类中的一个,我发布的错误特别引用了这个类/表,但所有其他类的设置都类似,并有类似的错误消息:

package com.algonquincollege.cst8277.assignment3.model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

/**
* The persistent class for the PORTFOLIO database table.
* 
*/
@Entity
@NamedQuery(name="Portfolio.findAll", query="SELECT p FROM Portfolio p")
public class Portfolio extends ModelBase implements Serializable {
private static final long serialVersionUID = 1L;
private List<Asset> assets;
public Portfolio() {
}
@Override
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="PORTFOLIO_ID", insertable=false, updatable=false)
public int getId() {
return this.id;
}

//bi-directional many-to-one association to Asset
@OneToMany(mappedBy="portfolio")
public List<Asset> getAssets() {
return this.assets;
}
public void setAssets(List<Asset> assets) {
this.assets = assets;
}
public Asset addAsset(Asset asset) {
getAssets().add(asset);
asset.setPortfolio(this);
return asset;
}
public Asset removeAsset(Asset asset) {
getAssets().remove(asset);
asset.setPortfolio(null);
return asset;
}
}

尝试将id字段的insertable更改为true。像这样:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="PORTFOLIO_ID", insertable=true, updatable=false)
public int getId() {
return this.id;
}

最新更新