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

线程"main"中的异常org.hibernate.注释异常:@OneToOne或@ManyToOne

邓韬
2023-03-14

我想在hibernate中进行一对多映射,但它显示出异常,我尝试了很多,但找不到解决方案

我有下面两张桌子

deptno--数字主键dname--varchar2(10)
loc--varchar2(10)

empno--数字主键
用户名--varchar2(10)
deptno--数字(10)外键

当我没有在Dept.java类上添加@Entity注释时,我会在stacktrace下面看到

21:35:11.554 [main] DEBUG o.h.i.f.DefaultIdentifierGeneratorFactory - Registering IdentifierGenerator strategy [enhanced-table] -> [class org.hibernate.id.enhanced.TableGenerator]
21:35:11.554 [main] INFO  org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
21:35:11.554 [main] INFO  org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
21:35:11.632 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
21:35:11.632 [main] WARN  org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
21:35:11.632 [main] DEBUG org.hibernate.util.DTDEntityResolver - attempting to resolve on classpath under org/hibernate/
21:35:11.632 [main] DEBUG org.hibernate.util.DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:XE
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - hibernate.connection.username=test
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - hibernate.connection.password=test
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - session-factory config [null] named class [com.beans.Employee] for mapping
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - session-factory config [null] named class [com.beans.Dept] for mapping
21:35:11.663 [main] INFO  org.hibernate.cfg.Configuration - Configured SessionFactory: null
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - properties: {hibernate.connection.password=test, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_17\jre\bin, java.vm.version=23.7-b01, hibernate.connection.username=test, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.script=, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=, java.vm.specification.name=Java Virtual Machine Specification, user.dir=E:\javahyd\eclipse_new_hibernate\One_Many1, java.runtime.version=1.7.0_17-b02, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Program Files\Java\jdk1.7.0_17\jre\lib\endorsed, os.arch=amd64, java.io.tmpdir=C:\Users\rajeev\AppData\Local\Temp\, line.separator=
, java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 7, sun.jnu.encoding=Cp1252, java.library.path=C:\Program Files\Java\jdk1.7.0_17\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/bin/server;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/bin;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/lib/amd64;C:\Windows\SYSTEM32\WBEM;C:\Program Files\Java\jdk1.7.0_17\bin;C:\ORACLEXE\APP\ORACLE\PRODUCT\10.2.0\SERVER\BIN;.;.;C:\PROGRAM FILES\MYSQL\MYSQL SERVER 5.5\BIN;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\Dell\DW WLAN Card;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\apache-maven-3.2.1\bin;C:\Windows\System32;.;E:\sts\sts-bundle\sts-3.6.0.M1;;., java.specification.name=Java Platform API Specification, java.class.version=51.0, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, os.version=6.1, user.home=C:\Users\rajeev, user.timezone=Asia/Calcutta, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.7, hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, java.class.path=E:\javahyd\eclipse_new_hibernate\One_Many1\target\classes;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\annotations-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\catalina-ant.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\catalina-ha.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\catalina-tribes.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\catalina.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\ecj-3.7.2.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\el-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jasper-el.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jasper.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jsp-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-coyote.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-dbcp.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-i18n-es.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-i18n-fr.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-i18n-ja.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-jdbc.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\tomcat-util.jar;C:\Users\rajeev\.m2\repository\ch\qos\logback\logback-core\1.1.2\logback-core-1.1.2.jar;C:\Users\rajeev\.m2\repository\ch\qos\logback\logback-classic\1.1.2\logback-classic-1.1.2.jar;C:\Users\rajeev\.m2\repository\org\slf4j\slf4j-api\1.7.6\slf4j-api-1.7.6.jar;C:\Users\rajeev\.m2\repository\com\myoracle\ojdbc14\10.2.0.1.0\ojdbc14-10.2.0.1.0.jar;C:\Users\rajeev\.m2\repository\org\hibernate\hibernate-core\3.6.10.Final\hibernate-core-3.6.10.Final.jar;C:\Users\rajeev\.m2\repository\antlr\antlr\2.7.6\antlr-2.7.6.jar;C:\Users\rajeev\.m2\repository\commons-collections\commons-collections\3.1\commons-collections-3.1.jar;C:\Users\rajeev\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\rajeev\.m2\repository\org\hibernate\hibernate-commons-annotations\3.2.0.Final\hibernate-commons-annotations-3.2.0.Final.jar;C:\Users\rajeev\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.0-api\1.0.1.Final\hibernate-jpa-2.0-api-1.0.1.Final.jar;C:\Users\rajeev\.m2\repository\javax\transaction\jta\1.1\jta-1.1.jar;C:\Users\rajeev\.m2\repository\javassist\javassist\3.12.1.GA\javassist-3.12.1.GA.jar, user.name=rajeev, hibernate.bytecode.use_reflection_optimizer=false, java.vm.specification.version=1.7, sun.java.command=com.test.Test, java.home=C:\Program Files\Java\jdk1.7.0_17\jre, sun.arch.data.model=64, hibernate.dialect=org.hibernate.dialect.Oracle10gDialect, hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:XE, user.language=en, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, java.version=1.7.0_17, java.ext.dirs=C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext;C:\Windows\Sun\Java\lib\ext, sun.boot.class.path=C:\Program Files\Java\jdk1.7.0_17\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_17\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_17\jre\classes, java.vendor=Oracle Corporation, file.separator=\, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=amd64}
21:35:11.663 [main] DEBUG org.hibernate.cfg.Configuration - Preparing to build session factory with filters : {}
21:35:11.741 [main] DEBUG org.hibernate.cfg.Configuration - Processing hbm.xml files
21:35:11.741 [main] DEBUG org.hibernate.cfg.Configuration - Process annotated classes
21:35:11.756 [main] INFO  org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.beans.Employee
21:35:11.789 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3DiscriminatorColumn{logicalColumnName'DTYPE', discriminatorTypeName='string'}
21:35:11.804 [main] DEBUG org.hibernate.cfg.AnnotationBinder - no value specified for 'javax.persistence.sharedCache.mode'; using UNSPECIFIED
21:35:11.820 [main] DEBUG o.h.cfg.annotations.EntityBinder - Import with entity name Employee
21:35:11.820 [main] INFO  o.h.cfg.annotations.EntityBinder - Bind entity com.beans.Employee on table employee1
21:35:11.867 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(employee1), mappingColumn=empno, insertable=true, updatable=true, unique=false}
21:35:11.882 [main] DEBUG o.h.cfg.annotations.PropertyBinder - binding property empno with lazy=false
21:35:11.882 [main] DEBUG o.h.c.annotations.SimpleValueBinder - building SimpleValue for empno
21:35:11.882 [main] DEBUG o.h.cfg.annotations.PropertyBinder - Building property empno
21:35:11.898 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3JoinColumn{logicalColumnName='deptno', referencedColumn='', mappedBy=''}
21:35:11.898 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(employee1), mappingColumn=dept, insertable=true, updatable=true, unique=false}
21:35:11.898 [main] DEBUG o.h.cfg.annotations.PropertyBinder - Building property dept
21:35:11.898 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(employee1), mappingColumn=deptno, insertable=true, updatable=true, unique=false}
21:35:11.898 [main] DEBUG o.h.cfg.annotations.PropertyBinder - binding property deptno with lazy=false
21:35:11.898 [main] DEBUG o.h.c.annotations.SimpleValueBinder - building SimpleValue for deptno
21:35:11.898 [main] DEBUG o.h.cfg.annotations.PropertyBinder - Building property deptno
21:35:11.898 [main] DEBUG org.hibernate.cfg.Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(employee1), mappingColumn=userName, insertable=true, updatable=true, unique=false}
21:35:11.898 [main] DEBUG o.h.cfg.annotations.PropertyBinder - binding property userName with lazy=false
21:35:11.898 [main] DEBUG o.h.c.annotations.SimpleValueBinder - building SimpleValue for userName
21:35:11.898 [main] DEBUG o.h.cfg.annotations.PropertyBinder - Building property userName
21:35:11.898 [main] DEBUG o.h.c.annotations.SimpleValueBinder - Setting SimpleValue typeName for empno
21:35:11.898 [main] DEBUG o.h.c.annotations.SimpleValueBinder - Setting SimpleValue typeName for deptno
21:35:11.898 [main] DEBUG o.h.c.annotations.SimpleValueBinder - Setting SimpleValue typeName for userName
21:35:11.898 [main] DEBUG org.hibernate.cfg.Configuration - processing fk mappings (*ToOne and JoinedSubclass)
Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.beans.Employee.dept references an unknown entity: com.beans.Dept
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1580)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1503)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1419)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
    at com.test.Test.main(Test.java:17)

当我在Dept.java类上添加@Entity注释时,我会在stacktrace下面看到

Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.beans.Employee column: deptno (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at com.test.Test.main(Test.java:17)
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="hibernate.connection.username">test</property>
        <property name="hibernate.connection.password">test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <mapping class="com.beans.Employee"></mapping>
        <mapping class="com.beans.Dept"></mapping>
    </session-factory>
</hibernate-configuration>
package com.beans;

import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Dept {
    @Id
    private int deptno;

    private String dname;

    private String loc;

    @OneToMany(mappedBy = "dept")
    Set<Employee> set;

    public Set<Employee> getSet() {
        return set;
    }

    public void setSet(Set<Employee> set) {
        this.set = set;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

}
package com.beans;

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

@Entity
@Table(name = "employee1")
public class Employee {
    @Id
    private int empno;

    private String userName;

    private int deptno;

    @ManyToOne
    @JoinColumn(name = "deptno")
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

}
package com.test;

import java.util.HashSet;
import java.util.Set;

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

import com.beans.Dept;
import com.beans.Employee;

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

        Employee employee = new Employee();
        Dept dept = new Dept();
        Set<Employee> set = new HashSet<Employee>();
        set.add(employee);

        dept.setDeptno(11);
        dept.setDname("mca");
        dept.setLoc("sbp");
        dept.setSet(set);
        session.save(dept);

        employee.setEmpno(1);
        employee.setUserName("rajeev");
        employee.setDept(dept);
        session.save(employee);

        transaction.commit();
        session.close();
    }
}

有人能帮我找到错误吗...下面是githib链接
git@github.com: mcarajeev/hibernate3.git

共有2个答案

匡旭东
2023-03-14

查看实体时,不应引发此异常,但是,当我检查git上的代码时,Dept类上缺少@Entity注释,您确定要使用@Entity吗?

请注意,当您映射一个Hibernate不知道关闭的实体时,会引发此异常。就像在您的案例中一样,员工与部门有关联,但部门不知道作为实体Hibernate,因为您没有使用@Entity。

private int deptno;

@ManyToOne
@JoinColumn(name = "deptno")
private Dept dept;

删除私有int deptno,hibernate将生成它作为关联的一部分。

万俟靖
2023-03-14

在您的员工中。java您已经添加了deptno作为联接列,并且您已经添加了deptno作为列,因此它正在给出异常的同一个表中创建两个deptno

解决方案:您可以从员工中删除私有int deptno。java作为联接列将用作外键。

 类似资料:
  • 问题内容: 每当我运行此命令时,该函数就可以正常使用。当我选择洞穴时,消息会每隔2秒弹出一次,然后当它越过该部分时,就会出现错误: 我已经尝试过和,并且在该方法中使用时,出现了很多错误。当我在方法中使用时,它不接受我的输入。 当我在该方法中使用时,它不接受我的字符串输入,而直接进入另一个游戏,但是布尔值返回并且它无限地发送垃圾邮件“ Which Cave …”。 我已经阅读了错误报告,以及类似问题

  • 问题内容: 我正在开发一个访问数据库的项目,但是我遇到了一些问题。我尝试使用hibernate3.2和4.52,但是它不起作用。 例外是在这行代码中 问题答案: 您需要在类路径中检查类org.apache.log4j.Level的冲突版本并进行解决。版本1.2.12或更高版本的log4j jar中提供了TRACE级别。

  • 我最近安装了intellij IDEA 14.0,为了确保一切正常,我创建了一个简单的Hello World程序。我不明白为什么输出不正确,为什么会出现这个错误。如果有人能帮忙,那就太好了。 以下是程序: 这是错误:

  • 问题内容: 当我运行程序进行智能卡读取时,会出现此异常。我的设备未连接。请帮我。 问题答案: 这意味着它无法加载您需要的共享库。这可能是因为。 该库不在您的库路径中。 该库名称不正确,例如,Unix上LIBRARY必须为libLIBRARY.so。 您无法执行该库。 该库不适用于操作系统或JVM的位大小。例如64位JVM将不会加载32位库。 您的JRE未正确安装,并且无法加载其自己的库之一。 您正

  • 我不断收到错误消息: 线程“main”java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.lang.Integer.ParseInt(Integer.java:580)在java.lang.Integer.ParseInt(Integer.java:615)在TestClass.Mai

  • 运行代码后,我收到以下错误 线程 “main” java.time.format.DateTimeParseException 中的异常:文本 '03-09-1999' 无法在 java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051) at java.base/java.time