zoukankan      html  css  js  c++  java
  • 关于Hibernate中多表关联查询的注释中参数的小细节

    本问题主要讨论范围及开发工具

    1. 注释形式的Hibernate多表关联查询

    2. 用的是idea这个开发工具(智能纠错简直不要太强了)

    3. 关联的两张表的主外键字段名称不一致(一致的话问题就没有了)

    错误形式

    我这里有两张表,一张是用户表usernote,一张是用户笔记表note,搭好Hibernate环境之后,想要关联查询两张表,在实体类上加了注释

      1 @Data
      2 @Entity
      3 @Table(name = "note")
      4 public class Note implements Serializable {//这是我的笔记表
      5 
      6     @Id
      7     @Column(name = "id")
      8     @GeneratedValue(generator = "identity")
      9     private Integer id;
     10     @Column(name = "context")
     11     private String Context;
     12     @Column(name = "publish_time")
     13     private Date publishTime;
     14     @Column(name = "user_id")//外键字段,关联用户表,注意表中字段名称是user_id
     15     private Integer userId;
     16     @Column(name = "like_count")
     17     private Integer likeCount;
     18 
     19     @ManyToOne(optional = false) //二者关系是多对一
     20     @JoinColumn(name = "user_id",
     21 	insertable = false,updatable = false,referencedColumnName = "userId")
     22     private UserNote userNote;
     23 
     24 }
     25 
     26 
    映射实体类Note
      1 @Entity
      2 @Table(name = "usernote")
      3 @Data
      4 public class UserNote {//这是我的用户表
      5 
      6     @Id
      7     @Column(name = "userId")
      8     @GeneratedValue(generator = "identity")
      9     private Integer userId;//我的关联字段,也是用户表的主键,注意字段名是userId
     10     @Column(name = "username")
     11     private String username;
     12     @Column(name = "address")
     13     private String address;
     14     @Column(name = "phone")
     15     private String phone;
     16 
     17     @OneToMany
     18      /************************最开始的错误形式******************************/
     19     /**@JoinColumn(name = "userId",referencedColumnName = "user_id")**/
     20     /************************最开始的错误形式******************************/
     21     @JoinColumn(name = "user_id",referencedColumnName = "userId")
     22     private List<Note> notes;
     23 }
     24 

    image

    在idea上(前提是配置了相关的设置),两个地方都飘红,最开始还没在意就运行了,部署过程直接失败,结果控制台报错

    Caused by: org.hibernate.MappingException: Unable to find column with logical

    name: user_id in org.hibernate.mapping.Table(usernote) and its related

    supertables and secondary tables

    解决

    解决的话就是讲@JoinColumn里面的name属性值改成外键字段就行,为此还特意去看了一下name属性的注释,如下

    (Optional) The name of the foreign key column. The table in which it is found depends upon the context.
    If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
    If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.
    If the join is for a ManyToMany mapping or for a OneToOne or bidirectional ManyToOne/OneToMany mapping using a join table, the foreign key is in a join table.
    If the join is for an element collection, the foreign key is in a collection table.
    Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "_"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; "_"; the name of the referenced primary key column.

    英文水平有限,也就看到这个红色部分。

    老实说idea的源码下载还有纠错功能真心不错Pointing up

  • 相关阅读:
    Java 并发工具包 java.util.concurrent 用户指南
    Java 序列化Serializable详解(附详细例子)
    Spring之FactoryBean .
    《用chsh选择shell》-linux命令五分钟系列之十二
    《vi中的替换艺术》-linux命令五分钟系列之十一
    0-1背包问题
    Java关键字final、static使用总结
    《作业控制系列》-“linux命令五分钟系列”之十
    《zip命令》-linux命令五分钟系列之九
    《bunzip2命令》-linux命令五分钟系列之八
  • 原文地址:https://www.cnblogs.com/marshwinter/p/10542043.html
Copyright © 2011-2022 走看看