gpt4 book ai didi

java - hibernate:OneToMany RelationShip:实体表不接受多个值

转载 作者:行者123 更新时间:2023-12-01 16:21:49 25 4
gpt4 key购买 nike

任何人都可以帮助我在我的 hibernate 程序中尝试实现 OneToMany 实体关系。我成功解决了第一步中的许多错误/异常,现在陷入了这个 Mysql 异常,并自 2 天以来一直在尝试解决

enter image description here

我只是想做这样的事情

enter image description here

我一步步提到我所做的事情

* 表创建

  create table area(acode varchar(100) not null, aname varchar(50) default null, primary key(acode));

create table employee (acode varchar(100) not null,ecode varchar(50) not null, primary key(ecode), name varchar(50) default null, key employee_foregin(acode), constraint employee_foregin foreign key(acode) references area(acode));




  • 添加 Maven 依赖项*
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mishra.practice</groupId>
<artifactId>HibernateDemo_1</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.15.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>

</dependencies>
</project>
<小时/>
  • 区域实体*


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Area {
@Id
private String aCode;
private String aName;
@ManyToOne
private Employee employee;

public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getaCode() {
return aCode;
}
public void setaCode(String aCode) {
this.aCode = aCode;
}
public String getaName() {
return aName;
}
public void setaName(String aName) {
this.aName = aName;
}

public Area() {
super();
// TODO Auto-generated constructor stub
}
public Area(String aCode, String aName) {
super();
this.aCode = aCode;
this.aName = aName;
}

public Area(String aCode) {
super();
this.aCode = aCode;
}


@Override
public String toString() {
return "Area [aCode=" + aCode + ", aName=" + aName + "]";
}


}
  • 员工实体*

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Employee {
@Id
private String eCode;
private String eName;

@OneToMany(mappedBy = "employee")
private Collection<Area> areas;

public Collection<Area> getAreas() {
return areas;
}

public void setAreas(Collection<Area> areas) {
this.areas = areas;
}

public String geteCode() {
return eCode;
}

public void seteCode(String eCode) {
this.eCode = eCode;
}

public String geteName() {
return eName;
}

public void seteName(String eName) {
this.eName = eName;
}

public Employee() {
super();
// TODO Auto-generated constructor stub
}

public Employee(String eCode, String eName, Collection<Area> areas) {
super();
this.eCode = eCode;
this.eName = eName;
this.areas = areas;

}

public Employee(String eCode, String eName) {
super();
this.eCode = eCode;
this.eName = eName;
}

@Override
public String toString() {
return "Employee [eCode=" + eCode + ", eName=" + eName + "]";
}

}

  • 进入区域
package relationMapping.one_2_Many;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class AreaEntry {
public static void main(String[] args) {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session= sessionFactory.openSession();
Transaction transaction= session.beginTransaction();

session.save(new Area("A111", "Indore_North"));
session.save(new Area("A112", "Indore_South"));
session.save(new Area("A113", "Indore_East"));
session.save(new Area("A114", "Bhopal_south"));
session.save(new Area("A115", "Bhopal_central"));

transaction.commit();
System.out.println("\n----------------All done-------------------");
session.close();
}
}
  • 员工条目
package relationMapping.one_2_Many;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmployeeEntry {
public static void main(String[] args) {
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session= sessionFactory.openSession();
Transaction transaction= session.beginTransaction();

List<Area> list1=new ArrayList<Area>();
list1.add(new Area("A101"));
list1.add(new Area("A102"));
session.persist(new Employee("E101", "Narendra",list1));



List<Area> list2=new ArrayList<Area>();
list2.add(new Area("A103"));
list2.add(new Area("A104"));
session.persist(new Employee("E102", "Nik", list2));

List<Area> list3=new ArrayList<Area>();
list3.add(new Area("A105"));
list3.add(new Area("A104"));
session.persist(new Employee("E103","Pradeep",list3));

List<Area> list4=new ArrayList<Area>();
list4.add(new Area("A104"));
list4.add(new Area("A105"));
session.persist(new Employee("E104", "Chetan", list4));

List<Area> list5=new ArrayList<Area>();
list5.add(new Area("A105"));
list5.add(new Area("A101"));
session.persist(new Employee("E105", "Dev", list5));

transaction.commit();
System.out.println("\n----------------All done-------------------");
session.close();
}
}

最佳答案

问题在于,在 Employee 表的 DDL 中,您定义了一个列 acode

   create table employee (acode varchar(100) not null,ecode varchar(50) not null, primary key(ecode), name varchar(50) default null,  key employee_foregin(acode),  constraint employee_foregin foreign key(acode) references area(acode));

此列未映射到您的 Employee Entity 类中的任何内容(但是您有一个名为 ecode 的列)

@Entity
public class Employee {
@Id
private String eCode;

这基本上就是错误消息告诉您的内容(未知列)

所以将Employee实体类中的ecode改为acode。顺便说一句,您应该让 Spring 为您生成数据库表以避免此类问题。

关于java - hibernate:OneToMany RelationShip:实体表不接受多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com