- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有 2 个表 employee
和 employee_document
。这是两个表的 mysql 查询 -
CREATE TABLE employee (
id int(11) unsigned NOT NULL,
name varchar(100) COLLATE utf8_unicode_ci NOT NULL,
email varchar(50) COLLATE utf8_unicode_ci NOT NULL,
password_hash varchar(100) COLLATE utf8_unicode_ci NOT NULL,
status int(11) NOT NULL,
creation_date bigint(20) NOT NULL,
created_by int(11) DEFAULT NULL,
update_date bigint(20) NOT NULL,
updated_by int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE employee_document (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
employee_id int(11) unsigned NOT NULL,
file_id int(11) unsigned NOT NULL,
document_type varchar(50) COLLATE utf8_unicode_ci DEFAULT '',
status int(11) DEFAULT NULL,
creation_date bigint(20) DEFAULT NULL,
created_by int(11) unsigned NOT NULL,
update_date bigint(20) DEFAULT NULL,
updated_by int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
在我的 spring boot 项目中,我将 hibernate 与 JPA 数据结合使用。以下是这些表的 java 解释。
@MappedSuperclass
public abstract class AbstractTimestampEntity {
@Transient
Logger log = LoggerFactory.getLogger(AbstractTimestampEntity.class);
@Column(name = "creation_date", nullable = false, updatable=false)
private Long creationDate;
@Column(name = "update_date", nullable = false)
private Long updateDate;
@PrePersist
protected void onCreate() {
log.debug("onCreate");
updateDate = creationDate = System.currentTimeMillis();
}
@PreUpdate
protected void onUpdate() {
log.debug("onUpdate");
updateDate = System.currentTimeMillis();
}
}
@Entity
@Table(name = "employee")
public class Employee extends AbstractTimestampEntity implements Serializable {
private static final long serialVersionUID = 1L;
public static final Integer STATUS_INACTIVE = 0;
public static final Integer STATUS_ACTIVE = 1;
public static final Integer STATUS_ARCHIVED = -1;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer id;
@Column(name = "name", nullable = false)
public String name;
@Email
@Column(name = "email", nullable = false, unique = true)
public String email;
@JsonIgnore
@Column(name = "password_hash", nullable = false)
public String passwordHash;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", fetch = FetchType.EAGER)
private Set<EmployeeDocument> documents;
@Max(1)
@Min(-1)
@Column(name = "status", nullable = false )
public Integer status;
@Column(name = "created_by", updatable = false)
public Integer createdById;
@Column(name = "updated_by")
public Integer updatedById;
}
@Entity
@Table(name = "employee_document")
public class EmployeeDocument extends AbstractTimestampEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Transient
public static final Integer STATUS_INACTIVE = 0;
@Transient
public static final Integer STATUS_ACTIVE = 1;
@Transient
public static final Integer STATUS_ARCHIVED = -1;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
public Employee employee;
@OneToOne
@JoinColumn(name = "file_id")
public EmployeeFile employeeFile;
@Max(1)
@Min(-1)
@Column(name = "status", nullable = false )
public Integer status;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "created_by", updatable = false)
@JsonBackReference
public Employee createdBy;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "updated_by")
@JsonBackReference
public Employee updatedBy;
}
此代码未执行,应用启动失败并抛出DuplicateMappingException
。这是完整的异常堆栈 -
org.hibernate.DuplicateMappingException: Table [employee_document] contains physical column name [created_by] referred to by multiple physical column names: [createdBy], [created_by]
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 org.hibernate.cfg.Ejb3Column.addColumnBinding(Ejb3Column.java:407)
at org.hibernate.cfg.Ejb3Column.linkWithValue(Ejb3Column.java:369)
at org.hibernate.cfg.annotations.SimpleValueBinder.linkWithValue(SimpleValueBinder.java:431)
at org.hibernate.cfg.annotations.SimpleValueBinder.make(SimpleValueBinder.java:407)
at org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:187)
at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:199)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2225)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:911)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:738)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 19 common frames omitted
我做错了什么?提前致谢。
最佳答案
您尝试从 EmployeeDocument 中引用 Empolyee 三次,但只有一次是通过主键引用的。其他两个由非主键列引用,您需要另外使用 referencedColumnName
选项才能使其工作:
@JsonIgnore
@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
public Employee employee;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "created_by", referencedColumnName="created_by", updatable = false)
@JsonBackReference
public Employee createdBy;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "updated_by". , referencedColumnName="updated_by")
@JsonBackReference
public Employee updatedBy;
关于java - created_by 字段上的 DuplicateMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873519/
我有一个抽象类和它的子类,我想使用 NHibernate 将它映射到我的数据库。我正在使用 Fluent以及如何进行映射。但是当我添加子类的映射时,在映射时会抛出 NHibernate.Duplica
我正在对两个类 MasavNew 和 MasavRow 使用 OneToMany 映射: @Entity @Table(name = "ArgoCHFileHdr") public class Mas
我正在 Spring Boot 中编写简单的“todo”应用程序。我在我的开发环境中使用 HSQLDB。 我有两个表“项目”和“任务”。任务表对项目表有一对多的引用,因为我想列出项目中的所有任务。 当
我有 2 个表 employee 和 employee_document。这是两个表的 mysql 查询 - CREATE TABLE employee ( id int(11) unsigned
我的域模型根中有一个类,如下所示: namespace Domain { public class Foo { ... } } 我在不同的命名空间中还有另一个同名的类: namespace Dom
编辑:我今天早上意识到异常似乎与 Spring 启动执行器有关,它似乎只在生产中运行,而不是在从 spring 工具套件启动项目时运行。 (查看帖子末尾的异常跟踪) 我有一个 spring boot
我试图强制 JPA/Hibernate 只生成和使用小写的表名。我已经实现了这样的 NamingStrategy: public class MyNamingStrategy extends Defa
我正在重写一个项目,但遇到了构建问题。 Caused by: org.hibernate.DuplicateMappingException: Table [language] contains ph
总结: 我想用 Fluent NHibernate Automapper 保存两个同名不同命名空间的类 上下文 我写的是必须将大量不同的对象导入数据库进行测试。我最终会将映射器写入适当的模型。 我一直
与NHibernate DuplicateMappingException when two classes have the same name but different namespaces相同
我正在尝试使用 hibernate 5.2.16 和 struts2-core-2.3.35 在带有 JDK8 的 Tomcat8.5 上运行 Spring Web 应用程序。 我收到以下异常: Ca
我正在创建一个在 Spring 框架、Tomcat、Hibernate(但只有 JPA API)上运行的 Java Web 应用程序。我编写了一些代码,这些代码使用外部化到 xml 文件中的命名查询连
我正在尝试使用 hibernate3.0 和 struts2-core-2.1.6 在 Tomcat7.0.55 上使用 运行一个 webapp >JDK8。我收到 org.hibernate.Dup
我正在使用下面的类,但给出了类似 Caused by: org.hibernate.DuplicateMappingException: Table [] contains physical colu
我是一名优秀的程序员,十分优秀!