我在Web上看到了很多使用Hibernate注释进行一对一共享主键映射的示例/教程。然而,在我看到的所有示例中,似乎映射不是可选的,或者如果它是可选的,则它不能在主键上。我有以下情况,我在公司表之间有一个主键映射 和一个CompanyDocs表。 CompanyDocs表是一个弱实体,即虽然存在公司对象,但它必然不会从一开始就链接到CompanyDocs实体,但这可能会在以后创建。
公司对象由外部应用程序创建和持久化,但使用Hibernate应用程序进行更新。 这就是我的......(请注意这不是实际的例子)
Company table
- companyid (Pk)
- name
- desc
- test1
- test2
CompanyDocs table
- docsid (Pk) (Fk)
- detail1
- detail2
- detail3
模型类
Company.java
@Entity
@Table(name = "company", schema = "public")
public class Company implements java.io.Serializable {
private String companyid
private CompanyDocs companydocs;
@Id
@Column(name = "companyid", unique = true, nullable = false, length = 20)
public String getCompanyid() {
return this.companyid;
}
public void setCompanyid(String companyid) {
this.companyid = companyid;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "companydocs")
@NotFound(action = NotFoundAction.IGNORE)
@PrimaryKeyJoinColumn(name = "companyid", referencedColumnName = "docsid")
public CompanyDocs getCompanydocs() {
return this.companydocs;
}
public void setCompanydocs(CompanyDocs companydocs) {
this.companydocs = companydocs;
}
}
CompanyDocs.java
@Entity
@Table(name = "companydocs", schema = "public")
public class CompanyDocs implements java.io.Serializable {
private String docsid;
private Company company;
@GenericGenerator(name = "generator", strategy = "foreign", parameters =
@Parameter(name = "property", value = "company"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "docsid", unique = true, nullable = false, length = 20)
public String getDocsid() {
return this.docsid;
}
public void setDocsid(String docsid) {
this.docsid = docsid;
}
@PrimaryKeyJoinColumn(name = "docsid", referencedColumnName = "companyid")
@OneToOne(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.IGNORE)
public Company getCompany() {
return this.company;
}
public void setCompany(Company company) {
this.company = company;
}
CompanyDAOImpl.java
public Company getCompany(String companyid) {
Criteria c = sessionFactory.getCurrentSession().createCriteria(Company.class)
.add(Restrictions.eq("companyid", companyid));
try {
return (Company) c.list().get(0);
}
catch(IndexOutOfBoundsException e) {
logger.info("GetCompany threw exception " +e);
return null;
}
}
public void storeCompany(Company c) {
sessionFactory.getCurrentSession().saveOrUpdate(c);
}
public void storeCompanyDocs(CompanyDocs cd) {
sessionFactory.getCurrentSession().saveOrUpdate(cd);
}
public CompanyDocs getCompanyDocs(String companyid) {
try{
Criteria c = sessionFactory.getCurrentSession()
.createCriteria(CompanyDocs.class).add(Restrictions.eq("companyid", companyid));
logger.info("getCompanyDocsreturned"+c.list().get(0));
return (CompanyDocs)c.list().get(0);
}catch(IndexOutOfBoundsException e){
logger.info("getCompanyDocs threw exception " +e);
return null;
}
}
CompanyServiceImpl.java
@Transactional(propagation = Propagation.REQUIRED, readOnly= false,
noRollbackFor=IllegalArgumentException.class)
@ExceptionHandler(NullPointerException.class)
public CompanyResponse updateCompany(Service parameters) throws SOAPException {
LOG.info("In updateCompany");
Company c = companyDao.getCompany(parameters.getId());
//CompanyDocs cd = companyDao.getCompanyDocs(parameters.getId());
CompanyDocs cd = c.getCompanyDocs();
LOG.info("CompanyDocs object is:- " + cd);
if(cd == null)
{
cd = new CompanyDocs(parameters.getId());
c.setCompanyDocs(cd);
cd.setCompany(c);
LOG.info("CompanyDocs object created new :- " + cd);
companyDao.storeCompanyDocs(cd);
LOG.info("CompanyDocs object stored :- " + cd.getDocsid());
}
c.setDetail1();
c.setDetail2();
c.setDetail3();
.............
companyDao.storeCompany(c)
LOG.info("Stored Company");
LOG.info("CompanyService Response Return------:")
return new CompanyResponse();
}
我的日志文件打印出来。
2012-05-02 22:57:18,951 INFO [au.com.CompanyServiceImpl] - <In updateCompany>
2012-05-02 22:57:18,975 INFO [au.com.CompanyDAOImpl] - <getCompany returned
au.com.Company@2738ef1d>[au.com.CompanyServiceImpl] - <CompanyDocs object is:- null>
2012-05-02 22:57:18,991 INFO [au.com.CompanyServiceImpl] - <CompanyDocs object
created new :- au.com.CompanyDocs@373ee92>
2012-05-02 22:57:18,995 INFO [au.com.CompanyServiceImpl] - <CompanyDocs object
updated :- CO12345>
2012-05-02 22:57:19,338 INFO [au.com.CompanyDAOImpl] - <Stored Company >
2012-05-02 22:57:19,338 INFO [au.com.CompanyServiceImpl] - <CompanyService
Response Return------:>
2012-05-02 22:57:19,498 ERROR [org.hibernate.jdbc.AbstractBatcher] - <Exception
executing batch: >
org.hibernate.StaleStateException: Batch update returned unexpected row count from
update [0]; actual row count: 0; expected: 1 at
org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at
org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener
.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.
onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.
doCommit(HibernateTransactionManager.java:656)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.
processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager
.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport
.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
at org.springframework.transaction.interceptor.TransactionInterceptor
.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation
.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.
invoke(JdkDynamicAopProxy.java:202)
at $Proxy28.login(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.
invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.cxf.service.invoker.AbstractInvoker.
performInvocation(AbstractInvoker.java:173)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)
at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:60)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.
run(ServiceInvokerInterceptor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at org.apache.cxf.workqueue.SynchronousExecutor.
execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.
handleMessage(ServiceInvokerInterceptor.java:106)
at org.apache.cxf.phase.PhaseInterceptorChain.
doIntercept(PhaseInterceptorChain.java:255)
at org.apache.cxf.transport.ChainInitiationObserver.
onMessage(ChainInitiationObserver.java:113)
at org.apache.cxf.transport.servlet.ServletDestination.
invoke(ServletDestination.java:97)
at org.apache.cxf.transport.servlet.ServletController.
invokeDestination(ServletController.java:461)
at org.apache.cxf.transport.servlet.ServletController.
invoke(ServletController.java:188)
at org.apache.cxf.transport.servlet.AbstractCXFServlet.
invoke(AbstractCXFServlet.java:148)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.
handleRequest(AbstractHTTPServlet.java:179)
at org.apache.cxf.transport.servlet.
AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.
service(AbstractHTTPServlet.java:159)
at org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.
doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.
invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.
invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.
invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.
process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
2012-05-02 22:57:19,501
ERROR [org.hibernate.event.def.AbstractFlushingEventListener]
- <Could not synchronize database state with session>
我尝试使用@non annotation,optional = false / true,也尝试使用单向映射并使用条件加载companydocs对象。但似乎Hibernate总是排除现有公司的CompanyDocs对象..
奇怪的是,当我们使用早期版本的hibernate并使用映射文件而不是注释时,这可以很好地工作。这是一个共享密钥映射,但是单向,因此公司hbm文件没有CompanyDocs对象的映射。 我正在使用hibernate 3.3.1.GA.jar进行注释。有没有我做错的事情,或者新版本的Hibernate不再可能这样做了吗?
答案 0 :(得分:1)
@PrimaryKeyJoinColumn
只应在可选方面使用,因此请移除@PrimaryKeyJoinColumn
上注释的Company
。
此外,mappedBy
的{{1}}属性应引用@OneToOne
标记的实体的属性名称。所以它应该是:
@OneToOne
答案 1 :(得分:1)
问题是Hibernate发布了更新,而不是companydocs对象的insert命令。这是因为如果对象的主键为null,那么Hibernate将执行插入,如果它不为null,它将进行更新,在我的情况下,因为我在实现类中设置了键,如下所示,它正在进行更新
原始代码
if(cd == null)
{
cd = new CompanyDocs(parameters.getId());
c.setCompanyDocs(cd);
cd.setCompany(c);
LOG.info("CompanyDocs object created new :- " + cd);
companyDao.storeCompanyDocs(cd);
LOG.info("CompanyDocs object stored :- " + cd.getDocsid());
}
新代码
if(cd == null)
{
cd = new CompanyDocs();//no-arg constructor
c.setCompanyDocs(cd);
cd.setCompany(c);
LOG.info("CompanyDocs object created new :- " + cd);
companyDao.storeCompanyDocs(cd);
LOG.info("CompanyDocs object stored :- " + cd.getDocsid());
}
工作得很漂亮!!
我发现此链接对解决此问题非常有用