欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA自关联 OneToMany小issue 《已解决》

程序员文章站 2022-04-22 21:55:44
...

 

PS: 答案

感谢 jinnianshilongnian同学,终于找到问题所以。

 

发现其实不是自关联的问题,所有的关系都是一样的存在这样的问题,只要注意一下就可避免。
问题出在:
Java persistence 允许两种注解方式去与数据库的table作映射: field和property .
经过测试,这两种方式是不能混着用的, 如果你选择在field上注解就不要再在getter方法上注解。
像我下面成功的代码里,注解都是在field上的,而出问题的代码是因为我的id是注解在field上而其它两个是注解在getter方法上。

总结 : 各字段的映射要么全在field上要么全在getter方法上,不能混用。

------------------------------------------------------------------------------------------------------------------------------

问题: 

 

        @Id
	@GeneratedValue
	private Integer id;
	private String name;
	@ManyToOne
	private Part bomPart;
	@OneToMany(mappedBy = "bomPart")
	private Collection<Part> parts;

 上面的代码没有问题

 而如果把注解加在getter方法上就会报错

 

       @Id
       @GeneratedValue
        private Integer id;
@OneToMany(mappedBy = "bomPart")
	public Collection<Part> getParts() {
		return parts;
	}

	public void setParts(Set<Part> parts) {
		this.parts = parts;
	}

	public Part getBomPart() {
		return bomPart;
	}
	@ManyToOne
	public void setBomPart(Part bomPart) {
		this.bomPart = bomPart;
	}
 

 

 

11:49:52,366 INFO  [org.jboss.as.osgi] (MSC service thread 1-5) JBAS011908: Unregister module: Module "deployment.EJBDBTest.jar:main" from Service Module Loader
11:49:52,379 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015877: Stopped deployment EJBDBTest.jar in 15ms
11:49:52,381 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "EJBDBTest.jar"
11:49:52,401 INFO  [org.jboss.as.jpa] (MSC service thread 1-2) JBAS011401: Read persistence.xml for test
11:49:52,408 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named DB in deployment unit deployment "EJBDBTest.jar" are as follows:

	java:global/EJBDBTest/DB!com.james.dao.DBRemote
	java:app/EJBDBTest/DB!com.james.dao.DBRemote
	java:module/DB!com.james.dao.DBRemote
	java:jboss/exported/EJBDBTest/DB!com.james.dao.DBRemote
	java:global/EJBDBTest/DB
	java:app/EJBDBTest/DB
	java:module/DB

11:49:52,423 INFO  [org.jboss.as.jpa] (MSC service thread 1-2) JBAS011402: Starting Persistence Unit Service 'EJBDBTest.jar#test'
11:49:52,425 INFO  [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-2) HHH000204: Processing PersistenceUnitInfo [
	name: test
	...]
11:49:52,442 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.persistenceunit."EJBDBTest.jar#test": org.jboss.msc.service.StartException in service jboss.persistenceunit."EJBDBTest.jar#test": Failed to start service
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0_33]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0_33]
	at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_33]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	... 3 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: part, for columns: [org.hibernate.mapping.Column(parts)]
	at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:305)
	at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:289)
	at org.hibernate.mapping.Property.isValid(Property.java:238)
	at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
	at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
	at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1736)
	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
	... 9 more

11:49:52,448 INFO  [org.jboss.as.osgi] (MSC service thread 1-2) JBAS011907: Register module: Module "deployment.EJBDBTest.jar:main" from Service Module Loader
11:49:52,554 INFO  [org.jboss.as.osgi] (MSC service thread 1-4) JBAS011908: Unregister module: Module "deployment.EJBDBTest.jar:main" from Service Module Loader
11:49:52,574 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015877: Stopped deployment EJBDBTest.jar in 20ms
11:49:52,579 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS015863: Replacement of deployment "EJBDBTest.jar" by deployment "EJBDBTest.jar" was rolled back with failure message {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"EJBDBTest.jar#test\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"EJBDBTest.jar#test\": Failed to start service
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory
    Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: part, for columns: [org.hibernate.mapping.Column(parts)]"},"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.deployment.unit.\"EJBDBTest.jar\".component.DB.START Missing[JBAS014861: <one or more transitive dependencies>]","jboss.naming.context.java.comp.EJBDBTest.EJBDBTest.DB.env.\"com.james.dao.DB\".em Missing[JBAS014861: <one or more transitive dependencies>]","jboss.deployment.unit.\"EJBDBTest.jar\".jndiDependencyService Missing[JBAS014861: <one or more transitive dependencies>]"]}
11:49:52,598 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) JBAS015876: Starting deployment of "EJBDBTest.jar"
11:49:52,623 INFO  [org.jboss.as.jpa] (MSC service thread 1-6) JBAS011401: Read persistence.xml for test
11:49:52,633 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-5) JNDI bindings for session bean named DB in deployment unit deployment "EJBDBTest.jar" are as follows:

	java:global/EJBDBTest/DB!com.james.dao.DBRemote
	java:app/EJBDBTest/DB!com.james.dao.DBRemote
	java:module/DB!com.james.dao.DBRemote
	java:jboss/exported/EJBDBTest/DB!com.james.dao.DBRemote
	java:global/EJBDBTest/DB
	java:app/EJBDBTest/DB
	java:module/DB

11:49:52,650 INFO  [org.jboss.as.osgi] (MSC service thread 1-1) JBAS011907: Register module: Module "deployment.EJBDBTest.jar:main" from Service Module Loader
11:49:52,650 INFO  [org.jboss.as.jpa] (MSC service thread 1-8) JBAS011402: Starting Persistence Unit Service 'EJBDBTest.jar#test'
11:49:52,651 INFO  [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-8) HHH000204: Processing PersistenceUnitInfo [
	name: test
	...]
11:49:52,663 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.persistenceunit."EJBDBTest.jar#test": org.jboss.msc.service.StartException in service jboss.persistenceunit."EJBDBTest.jar#test": Failed to start service
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0_33]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0_33]
	at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_33]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
	... 3 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: part, for columns: [org.hibernate.mapping.Column(parts)]
	at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:305)
	at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:289)
	at org.hibernate.mapping.Property.isValid(Property.java:238)
	at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
	at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
	at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1736)
	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
	... 9 more

11:49:52,666 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) JBAS014774: Service status report
JBAS014777:   Services which failed to start:      service jboss.persistenceunit."EJBDBTest.jar#test": org.jboss.msc.service.StartException in service jboss.persistenceunit."EJBDBTest.jar#test": Failed to start service
 

求解释!