Help hibernate

Prezados eu tenho as referencias abaixo e quando vou persistir a entidade PerfilAcesso o hibernate já que persistir AcessPerfilMenu.

Até ai tudo bem, até que gostei desse comportamento.

O problema é que AcessPerfilMenu para ser persistido precisa do id que acabou de ser gravado da entidade PerfilAcesso.

O hibernate não esta fazendo isso, nem sei se ele é capaz de fazer isso no meio da transação.

Segue códigos:
PerfilAcesso.java

@Entity
@Table(name = "perfil_acesso", catalog = "filesystem", schema = "public")
@XmlRootElement
public class PerfilAcesso implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Basic(optional = false)
	@Column(name = "perfil_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer perfilId;
	@Basic(optional = false)
	@NotNull
	@Size(min = 1, max = 256)
	@Column(name = "perfil_nome", nullable = false, length = 256)
	private String perfilNome;
	@Basic(optional = false)
	@NotNull
	@Column(name = "perfil_adm", nullable = false)
	private Character perfilAdm;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "acessPerfilId")
	private Collection<AcessPerfilMenu> acessPerfilMenuCollection;

AcessPerfilMenu .java

@Entity
@Table(name = "acess_perfil_menu", catalog = "filesystem", schema = "public")
@XmlRootElement
public class AcessPerfilMenu implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Basic(optional = false)
	@Column(name = "acess_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer acessId;
	@Basic(optional = true)
	@Column(name = "acess_salvar", nullable = true)
	private Character acessSalvar;
	@Basic(optional = true)
	@Column(name = "acess_excluir", nullable = true)
	private Character acessExcluir;
	@Basic(optional = true)
	@Column(name = "acess_alterar", nullable = true)
	private Character acessAlterar;
	@Basic(optional = true)
	@Column(name = "acess_pesquisar", nullable = true)
	private Character acessPesquisar;
	@JoinColumn(name = "acess_perfil_id", referencedColumnName = "perfil_id", nullable = false)
	@ManyToOne(optional = false)
	private PerfilAcesso acessPerfilId;
	@JoinColumn(name = "acess_menu_id", referencedColumnName = "men_id", nullable = false)
	@ManyToOne(optional = false)
	private Menu acessMenuId;

Método de persistência.

	@Override
	public PerfilAcesso save(PerfilAcesso obj, Integer usrId) throws Exception {
		if(!sessao.isOpen()){
			UtilHibernate util = new UtilHibernate();			
			sessao = util.getInstance();
		}	
		sessao.getTransaction().begin();
		try {
			sessao.flush();
			obj  = (PerfilAcesso) sessao.save(obj);				
			sessao.close();
			return obj;
		} catch (Exception e) {
			sessao.getTransaction().rollback();
			sessao.close();
			logger.error("Erro ao salvar! Obj = " + obj.toString()  + ", Erro = ", e);
			e.printStackTrace();
			return obj;
		}
	}

É capaz sim.

Tá dando erro?

Segue:


14:09:29,297 ERROR [PerfilAcessoDao     ] Erro ao salvar! Obj = br.com.filesystem.entities.PerfilAcesso[ perfilId=2, perfilNome=poip, perfilAdm=0 ], Erro = 
org.hibernate.exception.ConstraintViolationException: could not insert: [br.com.filesystem.entities.AcessPerfilMenu]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
	at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
	at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
	at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
	at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
	at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
	at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
	at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
	at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
	at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
	at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
	at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
	at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:475)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:353)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
	at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
	at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
	at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
	at br.com.filesystem.estructure.CrudDaoImplements.save(CrudDaoImplements.java:32)
	at br.com.filesystem.controller.PerfilAcessoController.salvar(PerfilAcessoController.java:76)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.filesystem.interceptores.LoginInterceptor.intercept(LoginInterceptor.java:33)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:71)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept(ParametersInstantiatorInterceptor.java:93)
	at br.com.caelum.vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:59)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:83)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:69)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56)
	at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54)
	at br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:44)
	at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:91)
	at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58)
	at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:88)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "acess_perfil_id" viola a restrição não-nula
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
	at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
	at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
	at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
	... 84 more

E se você colocar cascade persist acessPerfilId e persistir AcessPerfilMenu?

Esse que o detalhe eu precisava entender, sobre o cascade.

Eu li ontem artigos mas ficou nebuloso ainda.

Como você viu no erro, ele esta com problemas justamente por não ter colocado o id que ele acabou de criar na transação.

Você entende bem desse parâmetro cascade?

Prometo para você que se você ler no link abaixo e não entender, eu não vou saber te explicar:
JPA: Mini Livro - Primeiros passos e conceitos detalhados

MAuahahua [=

[quote=Hebert Coelho]Prometo para você que se você ler no link abaixo e não entender, eu não vou saber te explicar:
JPA: Mini Livro - Primeiros passos e conceitos detalhados

MAuahahua [=[/quote]

kkkkkkkkkkk, valeu cara.

Assim que eu resolver eu fecho a postagem.