gpt4 book ai didi

java - 建立实体管理器(Eclipse/Java)时,Hibernate 与 H2 出现错误

转载 作者:行者123 更新时间:2023-12-04 11:00:05 25 4
gpt4 key购买 nike

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

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 field 。像这样:

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

关于java - 建立实体管理器(Eclipse/Java)时,Hibernate 与 H2 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58866017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com