解决:DuplicateMappingException: Table [xx] contains physical column name referred to by multiple ... .

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

报错如题:

Caused by: org.hibernate.DuplicateMappingException: Table [xxx] contains physical column name [comment] referred to by multiple physical column names: [comment], [auditComment]
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.bindPhysicalToLogical(InFlightMetadataCollectorImpl.java:922)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.addBinding(InFlightMetadataCollectorImpl.java:891)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:961)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:942)
	at ... ...

 

情况1. 项目用的 spring-data-jpa 。

原本对于表和本类的每个字段都有给注解 @Colum 来对应,其实不用给也可以。把这些注解注释掉工程就可以正常启动了。

如下:


import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;

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

/**
 * 解冻申请
 */
@Entity(name = "unfreeze")
@Table(name = "unfreeze_apply")
@JsonIgnoreProperties(ignoreUnknown = true)
public class UnfreezeApply implements Serializable {

    private static final long serialVersionUID = -1826761763082698565L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

//    @Column(name = "employee_no", nullable = false)
    private String employeeNo;

    @Transient
    private String employeeName; //员工名称

    @ManyToOne(fetch = FetchType.EAGER)
    @JsonIgnore
//    @JoinColumn(name="employee_no", referencedColumnName="usernumber", insertable=false, updatable=false)
    private User employee;

//    @Column(name = "reason", nullable = false)
    private String reason;

}

启动成功:

 

情况2. (网上的其它情况)

 

当有一个物理表映射两个或多个逻辑表名称时,会发生此异常。如 因为那里有多个@JoinColumns使用相同的列名(user_id)

为每个JoinColumns(在注释中的name参数中定义)设置一个不同的名称:

@OneToOne
@JoinColumn(name = "local_user_id")
public User getUser() {
    return user;
}

@OneToOne
@JoinColumns({@JoinColumn(name = "userId", referencedColumnName = "userId"),
        @JoinColumn(name = "providerId", referencedColumnName = "providerId"),
        @JoinColumn(name = "providerUserId", referencedColumnName = "providerUserId")}
)
public UserConnection getUserConnection() {
    return userConnection;
}

 

参考:http://mussatto.github.io/java/hibernate/error/fix/mapping/2016/11/14/hibernate-error-multiple-physical-columns.html

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值