我在数据库中有两个表一对一关系:
CREATE TABLE IF NOT EXISTS `toproject`.`osoby` (
`id` INT NOT NULL AUTO_INCREMENT ,
`imie` VARCHAR(45) NOT NULL ,
`nazwisko` VARCHAR(45) NOT NULL ,
`pesel` VARCHAR(11) NOT NULL ,
`telefon` VARCHAR(45) NULL ,
`pensja` VARCHAR(45) NULL ,
`typ` VARCHAR(45) NOT NULL ,
`stanowisko_id` INT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `pesel_UNIQUE` (`pesel` ASC) ,
INDEX `fk_osoby_stanowiska_idx` (`stanowisko_id` ASC) ,
CONSTRAINT `fk_osoby_stanowiska`
FOREIGN KEY (`stanowisko_id` )
REFERENCES `toproject`.`stanowiska` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `toproject`.`adresy` (
`id` INT NOT NULL AUTO_INCREMENT,
`ulica` VARCHAR(45) NOT NULL ,
`miejscowosc` VARCHAR(45) NOT NULL ,
INDEX `fk_adres_osoby1_idx` (`id` ASC) ,
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_adres_osoby1`
FOREIGN KEY (`id` )
REFERENCES `toproject`.`osoby` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
我必须使用这个@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
我写的那个Java实体类:
班主任:
@Entity
@Table(name = "osoby")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="typ", discriminatorType=DiscriminatorType.STRING)
public class Osoba implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "imie")
private String imie;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "nazwisko")
private String nazwisko;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 11)
@Column(name = "pesel")
private String pesel;
客户端类:
@Entity
@DiscriminatorValue(value = "Klient")
public class Klient extends Osoba {
@Size(max = 45)
@Column(name = "telefon")
private String telefon;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "osobyId")
private Collection<Zamowienie> zamowienieCollection;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "klient")
private Adres adres;
地址实体:
@Entity
@Table(name = "adresy")
public class Adres implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "ulica")
private String ulica;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "miejscowosc")
private String miejscowosc;
@JoinColumn(name = "id", referencedColumnName = "id", insertable = false, updatable = false)
@OneToOne(optional = false)
private Klient klient;
我有一个klientMB,可以添加个人和地址
@ManagedBean
@RequestScoped
public class KlientMB {
@EJB
private KlientDaoLocal klientDao;
private Klient klient;
public KlientMB() {
}
public Klient getKlient() {
if (klient == null) {
klient = new Klient();
Adres adres = new Adres();
klient.setAdres(adres);
adres.setOsoba(klient);
}
return klient;
}
public void setKlient(Klient klient) {
this.klient = klient;
}
public String createKlient() {
klientDao.create(klient);
return "klienci.xhtml?faces-redirect=true";
}
但是当我尝试添加Klient时出现此错误:
javax.faces.el.EvaluationException:javax.ejb.EJBException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 在javax.faces.component.UICommand.broadcast(UICommand.java:315) 在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 在javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 在com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 在com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532) 在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513) 在java.lang.Thread.run(Thread.java:744) 引起:javax.ejb.EJBException at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) 在com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) 在com.sun.proxy。$ Proxy177.create(未知来源) at pl.ear.mb.KlientMB.createKlient(KlientMB.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) 在javax.el.BeanELResolver.invoke(BeanELResolver.java:528) 在javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) 在com.sun.el.parser.AstValue.invoke(AstValue.java:248) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ......还有32个 引起:javax.persistence.EntityExistsException:org.hibernate.exception.ConstraintViolationException:无法插入:[pl.ear.model.Adres] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:612) 在org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:269) 在pl.ear.dao.KlientDao.create(KlientDao.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 在org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 在com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer .__ intercept(BaseContainer.java:5360) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) ......还有46个 引起:org.hibernate.exception.ConstraintViolationException:无法插入:[pl.ear.model.Adres] 在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) 在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 在org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40) 在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158) 在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638) 在org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48) 在org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298) 在org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49) 在org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131) 在org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87) 在org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:609) 在org.hibernate.impl.SessionImpl.persist(SessionImpl.java:601) 在org.hibernate.engine.EJB3CascadingAction $ 1.cascade(EJB3CascadingAction.java:30) 在org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268) 在org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216) 在org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169) 在org.hibernate.engine.Cascade.cascade(Cascade.java:130) 在org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334) 在org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49) 在org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131) 在org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87) 在org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38) 在org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618) 在org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592) 在org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596) 在org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220) ......还有70多个 引发者:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(
toproject
。adresy
,CONSTRAINTFKAB3C9E7C7D93F9AD
FOREIGN KEY(id
)参考osoby
(id
)) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 在com.mysql.jdbc.Util.getInstance(Util.java:386) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) 在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) 在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) 在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) 在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) 在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) 在com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) 在org.hibernate.id.IdentityGenerator $ GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73) 在org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33) ... 99更多
答案 0 :(得分:0)
我认为问题在于您使用id
表中的toproject.adresy
(主键)列作为连接toproject.osoby
的外键。您应该引入一个新列(例如osoby_id)作为连接到toproject.osoby
的外键列。
目前发生的是,Adres
表的id用于查找Osoby
表中的记录,并且没有记录。