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

Hibernate中的ManyToMany映射引用错误(api生成的表名)Table Spring Boot

宁飞宇
2023-03-14

使用的技术有:Spring-boot、Spring-data-Jpa、Hibernate、MySql

完全错误为:

自定义视图实体是:

@Entity
@Table(name = "custom_views")
public class CustomViews implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -7325379776348403853L;

    @Id
    @GenericGenerator(name="generator", strategy="increment")
    @GeneratedValue(generator="generator")
    @Column(name="custom_id")
    private Long customId;

    @Column(name="view_name")
    private String viewName;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE},fetch=FetchType.LAZY)
    @JoinTable(name = "productcolumn_customfields", 
        joinColumns = { @JoinColumn(name = "cv_id") },
        inverseJoinColumns = { @JoinColumn(name = "pcd_id",nullable = false, updatable = false) })
    private List<ProductColumnsDetailsCV> productColumnsDetails;

    public CustomViews(){
        super();
    }

    public CustomViews(Long customId, String viewName, User user, List<ProductColumnsDetailsCV> productColumnsDetails) {
        super();
        this.customId = customId;
        this.viewName = viewName;
        this.user = user;
        this.productColumnsDetails = productColumnsDetails;
    }

    /**
     * @return the customId
     */
    public Long getCustomId() {
        return customId;
    }

    /**
     * @param customId the customId to set
     */
    public void setCustomId(Long customId) {
        this.customId = customId;
    }

    /**
     * @return the viewName
     */
    public String getViewName() {
        return viewName;
    }

    /**
     * @param viewName the viewName to set
     */
    public void setViewName(String viewName) {
        this.viewName = viewName;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @return the productColumnsDetails
     */
    public List<ProductColumnsDetailsCV> getProductColumnsDetails() {
        return productColumnsDetails;
    }

    /**
     * @param productColumnsDetails the productColumnsDetails to set
     */
    public void setProductColumnsDetails(List<ProductColumnsDetailsCV> productColumnsDetails) {
        this.productColumnsDetails = productColumnsDetails;
    }

}

产品列详细信息实体:

@Entity
@Table(name = "product_columns_details")
public class ProductColumnsDetailsCV implements Serializable{

    private static final long serialVersionUID = 7685428544522743567L;

    @Id
    @GenericGenerator(name="generator", strategy="increment")
    @GeneratedValue(generator="generator")
    @Column(name="pid")
    private Long pid;

    @Column(name="display_name")
    private String displayName;

    @Column(name="column_name")
    private String columnName;

    @Column(name="actual_name")
    private String actualName;

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

    @Column(name="is_pk")
    private boolean isPk;

    @Column(name="is_sortable")
    private boolean isSortable;

    @Column(name="order_")
    private int order;

    @Column(name="is_popup")
    private boolean isPopUp;


    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE})
    @JoinColumn(name="productColumnsDetails")
    private List<CustomViews> customviews;


    public ProductColumnsDetailsCV() {
        super();
    }


    /**
     * @param pid
     * @param displayName
     * @param columnName
     * @param actualName
     * @param customFilter
     */
    public ProductColumnsDetailsCV(Long pid, String displayName, String columnName, String actualName) {

        this.pid = pid;
        this.displayName = displayName;
        this.columnName = columnName;
        this.actualName = actualName;
    }

    //Getters & Setter

}

表格:

CREATE TABLE `productcolumn_customfields` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `pcd_id` bigint(20) DEFAULT NULL,
  `cv_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id_pid_productcoldetails_idx` (`pcd_id`),
  KEY `id_cvid_customviews_idx` (`cv_id`),
  CONSTRAINT `fk_pcd_id_productcolumndet` FOREIGN KEY (`pcd_id`) REFERENCES `product_columns_details` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `id_cvid_customviews` FOREIGN KEY (`cv_id`) REFERENCES `custom_views` (`custom_id`) ON DELETE CASCADE ON UPDATE CASCADE

CREATE TABLE `product_columns_details` (
  `pid` bigint(20) NOT NULL AUTO_INCREMENT,
  `display_name` varchar(255) DEFAULT NULL,
  `column_name` varchar(255) DEFAULT NULL,
  `actual_name` varchar(255) DEFAULT NULL,
  `type` varchar(25) NOT NULL DEFAULT 'String',
  `is_pk` bit(1) NOT NULL DEFAULT b'0',
  `is_sortable` bit(1) NOT NULL DEFAULT b'0',
  `order_` int(11) NOT NULL DEFAULT '0',
  `is_popup` bit(1) NOT NULL DEFAULT b'0',
  PRIMARY KEY (`pid`),
  UNIQUE KEY `actual_name_UNIQUE` (`actual_name`)

CREATE TABLE `custom_views` (
  `custom_id` bigint(30) NOT NULL AUTO_INCREMENT,
  `view_name` varchar(255) NOT NULL,
  `user_id` bigint(20) NOT NULL,
  PRIMARY KEY (`custom_id`),
  KEY `custom_views_ibfk_1` (`user_id`),
  CONSTRAINT `custom_views_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user`enter code here`_id`) ON DELETE CASCADE ON UPDATE CASCADE
)

共有1个答案

牛经赋
2023-03-14

这里出现问题是因为双方(在实体中)执行hibernate来执行多对多映射,这是错误的。这是resson hibernate假设连接表名为product_columns_details_customviews(不存在),并且不从配置(在实体CUSTOMVIEWS的注释中)获取表名,即productcolumn_customfields

现有代码:

@Entity
@Table(name = "product_columns_details")
public class ProductColumnsDetailsCV implements Serializable{
...
...
...
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name="productColumnsDetails")
private List<CustomViews> customviews;

而且

@Entity
@Table(name = "custom_views")
public class CustomViews implements Serializable{
...
...
...
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE},fetch=FetchType.LAZY)
@JoinTable(name = "productcolumn_customfields", 
    joinColumns = { @JoinColumn(name = "cv_id") },
    inverseJoinColumns = { @JoinColumn(name = "pcd_id",nullable = false, updatable = false) })
private List<ProductColumnsDetailsCV> productColumnsDetails;
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "productColumnsDetails")
private List<CustomViews> customviews;
 类似资料:
  • Django 1.5 PostgreSQL 9.2 psycopg2 2.4。6. 我正在使用QuerySet API中的额外功能,以便能够为Postgres使用多维数据集扩展中的函数-我知道出于可移植性的原因,额外功能不是很好,但无论如何我不会使用另一个DB(不是在Postgres之后,不是!)。所以问题是我从以下代码中得到了错误的SQL查询: 似乎psycopg2用单引号括住了表名,这对于Po

  • 我正在尝试使用@ManyToOne和@OneToMany映射实体表。映射列位于名为“internal_plan_id”的子表中。根据要求,我不能更改名称。下面是两个实体表:父表 儿童桌: 我得到错误为:错误:关系“financial_plan_subplan”的列“internal_plan_id_internal_plan_id”不存在。 financial_subplan中用于映射的现有列名是

  • 问题内容: 这是错误 我不明白为什么会引发此错误,应该对类进行映射,因为我将简要地向您展示。我有一个非常基本的配置,例如:http : //docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html 我试图将映射定义添加到hibernate.cfg.xml中,并且还尝试以编程方式添加它。他们俩都没有工作。有人可以告诉我我

  • 我在数据表和通过data_user表链接的用户表之间有一个Hibernate ManyToMany映射。现在我想更新数据表,以便在数据中再添加一个用户。如何更新链接表(data_user)为新用户添加一个条目? NonUniqueObjectException:一个具有相同标识符值的不同对象已经与会话相关联。 PS:我对冬眠很陌生。

  • 我将spring boot with JPA用于一个简单的CRUD应用程序。 我有一个带有一个,它有一个通过一对多关系引用两个基本的ID的复合主键。 但是,我得到一个错误,它说 org.hibernate.AnnotationException:针对未映射的类使用@OneTomany或@ManyTomany:Objecta.PrimaryKeys[PrimaryKey]。 这似乎是一个相当普遍的问