当前位置: 首页 > 知识库问答 >
问题:

无法访问实体类中的字段

锺离良哲
2023-03-14
import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the "city" database table.
 * 
 */
@Entity
@Table(name="\"city\"")
@NamedQuery(name="City.findAll", query="SELECT c FROM City c")
public class City extends com.dozie.example.util.Entity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="\"id\"")
    private Integer id;

    @Column(name="\"country_id\"")
    private int countryId;

    @Column(name="\"name\"")
    private String name;

    //bi-directional many-to-one association to Country
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="\"country_id\"", referencedColumnName="\"id\"")
        })
    private Country country;

    public City() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getCountryId() {
        return this.countryId;
    }

    public void setCountryId(int countryId) {
        this.countryId = countryId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Country getCountry() {
        return this.country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

对于我的另一个实体类

import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;


/**
 * The persistent class for the "country" database table.
 * 
 */
@Entity
@Table(name="\"country\"")
@NamedQuery(name="Country.findAll", query="SELECT c FROM Country c")
public class Country extends com.dozie.example.util.Entity implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private Set<City> cities;

    public Country() {
    }


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="\"id\"")
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    @Column(name="\"name\"")
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }


    //bi-directional many-to-one association to City
    @OneToMany(mappedBy="country")
    public Set<City> getCities() {
        return this.cities;
    }

    public void setCities(Set<City> cities) {
        this.cities = cities;
    }

    public City addCity(City city) {
        getCities().add(city);
        city.setCountry(this);

        return city;
    }

    public City removeCity(City city) {
        getCities().remove(city);
        city.setCountry(null);

        return city;
    }

}

这是我的数据库表的脚本,它是一个Postgres数据库

CREATE TABLE city
(
  id integer NOT NULL,
  country_id integer NOT NULL,
  name character varying(30) NOT NULL,
  CONSTRAINT city_pk PRIMARY KEY (id),
  CONSTRAINT city_country_fk FOREIGN KEY (country_id)
      REFERENCES country (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT city_name_idx UNIQUE (name)
)

CREATE TABLE country
(
  id integer NOT NULL,
  name character varying(10) NOT NULL,
  CONSTRAINT country_pk PRIMARY KEY (id),
  CONSTRAINT country_name_idx UNIQUE (name)
)

当我的服务器运行时,我会收到以下错误消息

我会很感激你帮我的忙的。

共有1个答案

楚志强
2023-03-14

你到处都加了双引号。你不应该。例如,

 @Table(name="\"city\"")

应该是

@Table(name="city")

而且

@Column(name="\"id\"")
@Column(name="id")
@JoinColumns({
    @JoinColumn(name="\"country_id\"", referencedColumnName="\"id\"")
    })
@JoinColumn(name="country_id")
 类似资料:
  • 在我的项目中,我使用XStream序列化/反序列化我的实体。 在应用程序中,所有的事情都很好,但当我尝试创建一些单元测试时就不行了。 我有一个抽象类,它有一个字段和公共访问器。我有一个类扩展了。 我的XStream配置如下所示: 当我运行我的应用程序时,没有任何问题。但当我尝试创建单元测试时,XStream引发异常: Edit 我在测试中尝试了,它引发了同样的异常。但当我尝试时,没有问题。我知道这

  • 问题内容: 我正在尝试在BigQuery上使用标准SQL方言(即不是传统SQL)运行查询。我的查询是: 但是要继续得到错误 有人知道正确的语法吗? 问题答案: 如果您正在寻找所有推荐人,请尝试

  • 我尝试使用下面的代码访问元模型实体属性/变量:

  • 我想让具有多个内部类的抽象类对其进行扩展,并可以通过静态方法创建其内部类的实例,但我得到编译器错误“无法访问ITask类型的封闭实例。必须使用ITask类型的封闭实例限定分配(例如,x.new A(),其中x是ITask的实例)。” 我发现内部类应该由外部类的实例创建,但是我的外部类有抽象方法,我不能创建它的实例。我创建了扩展父类的内部类,因为我习惯于控制它们的创建。那么有没有办法让我的模式发挥作

  • 我试图找到一种很好的方法来迭代并打印出ArrayList中的对象。 问题是我似乎无法到达用户键入对象信息(例如颜色和品牌)的字段。 对象的新实例如下所示: 汽车(以及自行车、公共汽车等其他类别)是汽车的子类。 车辆等级: } 汽车等级: }尝试迭代对象时: 当迭代对象时,我希望达到I.color中的颜色和品牌字符串。但是我猜想,由于我是一个新的对象,它可能无法访问与汽车、公共汽车等相关的字段和方法

  • 我有一个叫做GreenhouseControls的类,它有很多 内置于其中的类,例如: 我从文本文件中提取值以获取事件名称(如“ThermostatNight”)和时间值(如“2000”)。为了用这些值实例化一个新对象,我构建了一个EventFactory,它接受这些值作为参数。 这是我构建的类,用于从文本文件值创建新的事件对象: 当我运行这个程序时,一切都运行得很好,直到我将EventFacto