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

如何解决一对一关系的hibernate jpa错误?

戚正业
2023-03-14

您好,我正在尝试在Hibernate4.0中使用注释进行一对一的关系,但我出现了这个错误。

 Mar 15, 2014 11:45:09 PM
org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Mar 15, 2014 11:45:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.Final}
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 15, 2014 11:45:09 PM org.hibernate.internal.util.xml.DTDEntityResolveresolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Mar 15, 2014 11:45:09 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Mar 15, 2014 11:45:10 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export

alter table Student_Details.Employee 
    drop 
    foreign key FK_fv5220moxm8v5ehji5yi2y9w3

drop table if exists Student_Details.Employee

drop table if exists Student_Details.EmployeeDetail

drop table if exists Student_Details.Student

create table Student_Details.Employee (
    employeeid integer not null auto_increment,
    employeename varchar(255),
    empdetailid_fk integer,
    primary key (employeeid)
)

create table Student_Details.EmployeeDetail (
    employeedetailid integer not null auto_increment,
    city varchar(255),
    employeeDesgnation varchar(255),
    salary integer not null,
    primary key (employeedetailid)
)

create table Student_Details.Student (
    studentId integer not null,
    phoneNumber integer not null,
    studentName varchar(255),
    primary key (studentId)
)

alter table Student_Details.Employee 
    add constraint FK_fv5220moxm8v5ehji5yi2y9w3 
    foreign key (empdetailid_fk) 
    references Student_Details.EmployeeDetail (employeedetailid)
Mar 15, 2014 11:45:10 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Mar 15, 2014 11:45:10 PM         org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mar 15, 2014 11:45:10 PM         org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL[jdbc:mysql://localhost:3307]
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000006: Autocommit mode: false
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Mar 15, 2014 11:45:11 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Mar 15, 2014 11:45:11 PM     org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 15, 2014 11:45:11 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory<init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

我的代码是::-

package com.exp.oneToOne;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Employee {

private int employeeid;
private String employeename;
private EmployeeDetail emp_details;

public Employee() {

}

public Employee(String employeename, EmployeeDetail emp_details) {

    this.employeename = employeename;
    this.emp_details = emp_details;
}



@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "empdetailid_fk")
public EmployeeDetail getEmp_details() {
    return emp_details;
}

public void setEmp_details(EmployeeDetail emp_details) {
    this.emp_details = emp_details;
}

@Id
@GeneratedValue
public int getEmployeeid() {
    return employeeid;
}

public void setEmployeeid(int employeeid) {
    this.employeeid = employeeid;
}

public String getEmployeename() {
    return employeename;
}

public void setEmployeename(String employeename) {
    this.employeename = employeename;
}

}
package com.exp.oneToOne;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EmployeeDetail {

private int employeedetailid;
private String employeeDesgnation;
private int salary;
private String city;

public EmployeeDetail() {       

}
public EmployeeDetail(String employeeDesgnation,
        int salary, String city) {


    this.employeeDesgnation = employeeDesgnation;
    this.salary = salary;
    this.city = city;
}

@Id
@GeneratedValue
public int getEmployeedetailid() {
    return employeedetailid;
}

public void setEmployeedetailid(int employeedetailid) {
    this.employeedetailid = employeedetailid;
}

public String getEmployeeDesgnation() {
    return employeeDesgnation;
}

public void setEmployeeDesgnation(String employeeDesgnation) {
    this.employeeDesgnation = employeeDesgnation;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}
}
package com.exp.oneToOne;
import org.hibernate.Session;
import com.exp.first.MainConfig;
public class TestEmployee {
public static void main(String args[]) {
    Session session = MainConfig.mainsessionFactory().openSession();
    session.beginTransaction();
    EmployeeDetail empdetail = new EmployeeDetail("CEO", 250000,
            "Ahmedabad");
    Employee emp = new Employee("Chirag", empdetail);
    session.save(emp);
    session.getTransaction().commit();
    session.close();
}
}

Hibernate控制文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3307</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.default_schema">Student_Details</property>
    <property name="current_session_context_class">thread</property>
    <property name="connection.pool_size">2</property>

</session-factory>

共有1个答案

白昊东
2023-03-14

您应该对每个@entity类使用@table(Name=“table-name”)注释,如下所示

@Entity
@Table(name="employeedetail")
public class EmployeeDetail {

//Code inside your Entity class

}
 类似资料:
  • 让我们来理解MS Access中的一对一关系。 这种关系用于将一个表中的一条记录与另一个表中的一条记录相关联。 现在转到 数据库工具 选项卡。 点击关系 选项。然后选择和,然后单击添加按钮将它们添加到视图中,然后关闭显示表格对话框。如下图所示 - 要创建这两个表之间的关系,请使用鼠标,然后单击并按住Employees 中的字段,然后将该字段拖放到要关联的字段上,方法是将鼠标悬停在tblHRData

  • 问题内容: 我正在尝试在MySQL数据库中实现“一对一”的关系。例如,假设我有一个Users表和一个Accounts表。我想确保一个用户只能拥有一个帐户。每个用户只能有一个帐户。 我找到了两个解决方案,但是不知道该使用什么,还有其他选择。 第一个解决方案: 在此示例中,我在指向用户主键的帐户中定义外键。然后,我使外键成为唯一键,因此帐户中不能有两个相同的用户。要联接表,我将使用以下查询: 第二种解

  • 我试图在MySQL数据库中实现“一对一”的关系。例如,假设我有一个Users表和一个Accounts表。我想确保一个用户只能有一个帐户。每个用户只能有一个帐户。 我找到了两个解决方案,但不知道该用什么,还有其他的选择。 在本例中,我定义了指向用户中主键的帐户中的外键。然后我使外键唯一,所以帐户中不能有两个相同的用户。要联接表,我将使用以下查询: null 如果我将这些解决方案中的任何一个导入到My

  • 问题内容: 我在执行查询时遇到问题。我想做的是通过MyDomainB的查询来查询MyDomainA中“字符串代码”中的数据。表之间的关系是单向一对一的。 可以使用gorm方法来做到这一点吗? 域A: 表域A 域B :(具有单向关系) } 表域B 我想做的是知道有多少用户具有相同的代码(代码是DomainA的一列,并且两个表都具有我之前指出的单向关系)。 这是我正在尝试这样做是错误的…控制器: 我希

  • 我有一个组织表,部门表和员工表。一个组织有许多部门,一个部门有许多员工。在执行 (组织的 PK)时,我看到 Employee 表也被查询 n 次,其中 n 是组织的部门数。 如何避免在执行相同操作时获取员工数据?我在这里看到了一个N 1问题。但不确定如何在嵌套的一对多映射中避免它。 提取类型设置为“延迟”。

  • 问题内容: 我有一对一的关系,但是hibernatetool在生成模式时抱怨。这是显示问题的示例: 人与OtherInfo具有一对一关系: 人是OtherInfo的拥有方。OtherInfo是拥有方,因此person用于在Person中指定属性名称“ otherInfo”。 使用hibernatetool生成数据库架构时出现以下错误: 知道为什么吗?我是在做错什么还是这是Hibernate错误?