繁体   English   中英

无法加入两个表 JPA

Unable to join two table JPA

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有两个表,我尝试加入使用列idsubscriber但显示一些错误。

Class Subscriber

@Entity
@Table(name = "subscriber")
@XmlRootElement
@NamedQueries({
      @NamedQuery(name = "Subscriber.findAll", query = "SELECT s FROM Subscriber s")
    , @NamedQuery(name = "Subscriber.findById", query = "SELECT s FROM Subscriber s WHERE s.id = :id")
    , @NamedQuery(name = "Subscriber.findByIdsubscriber", query = "SELECT s FROM Subscriber s WHERE s.idsubscriber = :idsubscriber")
    , @NamedQuery(name = "Subscriber.findByName", query = "SELECT s FROM Subscriber s WHERE s.name = :name")
    , @NamedQuery(name = "Subscriber.findByPhone", query = "SELECT s FROM Subscriber s WHERE s.phone = :phone")
    , @NamedQuery(name = "Subscriber.findByLoc", query = "SELECT s FROM Subscriber s WHERE s.loc = :loc")
    , @NamedQuery(name = "Subscriber.findByNum", query = "SELECT s FROM Subscriber s WHERE s.num = :num")
    , @NamedQuery(name = "Subscriber.findByType", query = "SELECT s FROM Subscriber s WHERE s.type = :type")
    , @NamedQuery(name = "Subscriber.findByDate", query = "SELECT s FROM Subscriber s WHERE s.date = :date")})
public class Subscriber implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "idsubscriber", nullable = false, length = 50)
    private String idsubscriber;
    @Basic(optional = false)
    @Column(name = "name", nullable = false, length = 200)
    private String name;
    @Basic(optional = false)
    @Column(name = "phone", nullable = false, length = 45)
    private String phone;
    @Basic(optional = false)
    @Column(name = "loc", nullable = false, length = 255)
    private String loc;
    @Basic(optional = false)
    @Column(name = "num", nullable = false)
    private int num;
    @Column(name = "type")
    private Integer type;
    @Basic(optional = false)
    @Column(name = "date", nullable = false, length = 50)
    private String date;
    
     @OneToOne(targetEntity = Deon.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "idsubscriber",referencedColumnName="idsubscriber")
    private Deon deon;

    public Subscriber() {
    }

    public Subscriber(Integer id) {
        this.id = id;
    }

    public Subscriber(Integer id,String name, String phone,int type, String loc, int num, String date) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.type=type;
        this.loc = loc;
        this.num = num;
        this.date = date;
    }
    
    public Integer getId() {
        return id;
    }

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

    public String getIdsubscriber() {
        return idsubscriber;
    }

    public void setIdsubscriber(String idsubscriber) {
        this.idsubscriber = idsubscriber;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    public Deon getItems() {
        return deon;
    }

    public void setItems(Deon deon) {
        this.deon = deon;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Subscriber)) {
            return false;
        }
        Subscriber other = (Subscriber) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "hiber.Subscriber[ id=" + id + " ]";
    }
    
}

Deon迪昂:

@Entity
@Table(name = "deon", catalog = "mngp", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Deon.findAll", query = "SELECT d FROM Deon d")
    , @NamedQuery(name = "Deon.findById", query = "SELECT d FROM Deon d WHERE d.id = :id")
    , @NamedQuery(name = "Deon.findByIdsubscriber", query = "SELECT d FROM Deon d WHERE d.idsubscriber = :idsubscriber")
    , @NamedQuery(name = "Deon.findByPrice", query = "SELECT d FROM Deon d WHERE d.price = :price")
    , @NamedQuery(name = "Deon.findBySprice", query = "SELECT d FROM Deon d WHERE d.sprice = :sprice")
    , @NamedQuery(name = "Deon.findByDate", query = "SELECT d FROM Deon d WHERE d.date = :date")})
public class Deon implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "idsubscriber", nullable = false, length = 50)
    private String idsubscriber;
    @Basic(optional = false)
    @Column(name = "price", nullable = false)
    private int price;
    @Column(name = "sprice")
    private Integer sprice;
    @Basic(optional = false)
    @Column(name = "date", nullable = false, length = 50)
    private String date;

    public Deon() {
    }

    public Deon(Integer id) {
        this.id = id;
    }

    public Deon(Integer id, String idsubscriber, int price, String date) {
        this.id = id;
        this.idsubscriber = idsubscriber;
        this.price = price;
        this.date = date;
    }

    public Integer getId() {
        return id;
    }

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

    public String getIdsubscriber() {
        return idsubscriber;
    }

    public void setIdsubscriber(String idsubscriber) {
        this.idsubscriber = idsubscriber;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Integer getSprice() {
        return sprice;
    }

    public void setSprice(Integer sprice) {
        this.sprice = sprice;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Deon)) {
            return false;
        }
        Deon other = (Deon) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "another.Deon[ id=" + id + " ]";
    }
    
}

Class Main

List<Subscriber>ll=em.createQuery("SELECT s.name,d.price FROM Subscriber s join s.Deon d ").getResultList();
                 for(Subscriber f: ll){
                      System.out.println(f.getName()+ " -- " +f.getItems().getPrice());
                 }

错误:

org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [subscriber.idsubscriber].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[deon]
Descriptor: RelationalDescriptor(hiber.Subscriber --> [DatabaseTable(subscriber)])
2 个回复

不要在此处使用连接,而是可以如下重写该查询,因为 hibernate/JPA 会为您自己进行映射。

SELECT s.name , s.deon.price from subscriber s

错误的问题是您将订阅者“idsubscriber”列映射到两个单独的 java 属性 - “deon”OneToOne 属性和“idsubscriber”字符串属性。

如果您的意思是让 Deon 表有一个包含订阅者 ID 值的外键列,请尝试类似的方法:

public class Subscriber implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    .. //(all other properties mapped the same, excluding idsubscriber!)
    @OneToOne(mappedBy="subscriber", cascade = CascadeType.ALL)
    private Deon deon;
    ..
}


public class Deon implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    //If you really need a string for this:
    @Column(name = "idsubscriber", nullable = false, length = 50, updatable=false, insertable=false)
    private String idsubscriber;
    @JoinColumn(name = "idsubscriber",referencedColumnName="idsubscriber")
    private Subscriber subscriber;//this reference is what now controls and sets the "idsubscriber" fk column.
    .. //other properties, mappings and methods unchanged
}
3 JPA-联接两个表

我有两个表,即 一个用户可以具有多个角色。 一个角色可以映射到多个权限。 我有一个实体-USER,负责维护有关该用户的信息。 首次登录时通过LDAP(不是DB)获取此信息。 现在,对于我的授权方面,我还需要从上述表格中获取有关用户权限的dtl。 所以我可以想 ...

4 使用JPA从两个表联接到第三个表中的组合键

我想将Hibernate与JPA结合使用,在三个表之间进行联接,如下所示: 表格:[方括号中的主键] 明确地说,这意味着具有相同tool_id的不同工具带上的工具可以具有不同的名称。 这是我想要的SQL查询: 我如何用JPA做这样的事情? ...

5 JPA注释-如何将两个字段合并为联接表的一列?

因此,我有两个类,即Route和BusStop。 路线“使用”两个BusStop对象,一个作为起点,一个作为目的地。 我需要通过一个联接表“使用”来映射这种关系。 此联接表不需要区分将BusStop用于起点还是终点。 基本上,我只需要起点和终点就可以成为此联接表中的一列(允许重复), ...

6 JPA对两个OneToMany实体使用一个联接表

有一个实体类“ A”(假设是一个人),还有一个实体类“ B”(假设是一个合同)。 实体“ A”与类“ B”的关系为@OneToMany (一个人可以签署很多合同)。 实体“ B”也与类“ A”具有@OneToMany关系(合同可以有很多人对其进行签名)。 在这种情况下,数据库Joi ...

7 如何通过JPA加入表?

我有四个桌子。 邮政编码 邮递区号(PK) townCode 镇 townCode(PK) townName cityCode 市 cityCode(PK) 城市名 地区代码 州 县区代码(PK) 县名 我 ...

8 JPA-加入同一张桌子

我收到了这个请求: 但这不起作用,我得到了一条错误消息,告诉我们缺少一个假牙: 在第二次选择之后,JPA生成的SQL带有括号: 我不知道为什么冬眠第二select后添加括号? 如果您知道该怎么办,请... ...

9 JPA:连接表语法

给定以下实体(为简洁起见,此长定义中省略了一些列): 如何获得给定产品SKU的相关产品集? products_related表如下所示: 我知道如何使用SQL获得答案,但我是JPA的新手,所以我还没有完全了解API和查询语法。 ...

2010-11-15 02:07:18 1 4344   java/ jpa
10 JPA使用SELECT连接表

我有这样的SQL查询: 它返回my_table(其中有多个名为=“”)的行以及该名称的最大created_date。 有没有办法在JPQL查询中复制它? 这是Entity类的要点,它非常简单: ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM