gpt4 book ai didi

java - 线程中的异常 "main"org.hibernate.AnnotationException : @OneToOne or @ManyToOne

转载 作者:行者123 更新时间:2023-12-01 12:33:01 26 4
gpt4 key购买 nike

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

我有下面两张表

部门

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

员工1

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

当我没有在 Dept.java 类上添加 @Entity 注释时,我会低于堆栈跟踪

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 注释时,我得到了堆栈跟踪下方

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.cfg.xml

<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>

Dept.java

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;
}

}

员工.java

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;
}

}

测试.java

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();
}
}

谁能帮我找出错误...下面是github链接
git@github.com:mcarajeev/hibernate3.git

最佳答案

在 Employee.java 中,您已添加 deptno 作为连接列,并且已添加 deptno 作为列,因此它创建了两个 deptno > 在同一个表中给出异常。

解决方案:您可以从 Employee.java 中删除 private int deptno,因为您的 Join 列将用作外键。

关于java - 线程中的异常 "main"org.hibernate.AnnotationException : @OneToOne or @ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25793215/

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