Hibernate insert Composite ID

0 respostas
B

Olá, pessoal

Estou tendo um problema na hora de inserir dados em um banco com relacionamento @ManyToOne, vou descrever os mapeamentos e logo depois qual é o problema que estou encontrando

TABELAS

-------------
- projects -
-------------
(PK) id A.I.
name
create

----------------
- documents -
----------------
(PK) id A.I.
(PK) projects_id
...

Relacionamento -> projects 1 - > 1.* documents

ProjectsEntity.java

@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private Long id;
	
	@Column(name = "name", unique = true, nullable = false, length = 50)
	private String name;
	
	@Column(name = "create_data", nullable = false)
	private Long createData;

	@OneToMany(mappedBy="projects", fetch=FetchType.LAZY)
	private Set<DocumentsEntity> documentses = new HashSet<DocumentsEntity>(0);
....

DocumentsEntity.java

@EmbeddedId
	@AttributeOverrides( {
			@AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
			@AttributeOverride(name = "projectsId", column = @Column(name = "projects_id", nullable = false)) })
	private DocumentsId id;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "projects_id", insertable=false, updatable=false)
	private ProjectsEntity projects;
....
DocumentsId.java
@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name = "id", nullable = false)
	private Long id;
	
	@Column(name = "projects_id", nullable = false)
	private Long projectsId;
...

Insert

...
transaction.begin();
		
ProjectsEntity project = new ProjectsEntity();
project.setName("Teste1");
project.setCreateData( (new Date().getTime() / 1000) );

entityManager.persist( project );

DocumentsEntity documentsEntity = new DocumentsEntity();
documentsEntity.setId(new DocumentsId( null, project.getId() ));
documentsEntity.setName("TESTE DOC1");
documentsEntity.setProjects(project);

entityManager.persist( documentsEntity );

System.out.println("A:> " + documentsEntity.getId() );
System.out.println("B:> " + documentsEntity.getId().getId() );

transaction.commit();
...

Bom, com todos os mapeamentos descritos acima. Minha dúvida é a seguinte.

Estou conseguindo salvar o relacionamento Projects - Documents, porém não consigo ter o retorno do ID do Documento depois de salvo, assim como consigo o do projeto
Nesse caso a saída é
B: null

Já tentei fazer o

documentsEntity  = entityManager.merge( documentsEntity );

Mais tambem não retorna.

Queria saber oq tenho que fazer para obter esse retorno, pois ainda tenho outra tabela que usa os Ids do Documento para se relacionar.

Grato,
Brunno

Criado 20 de abril de 2010
Respostas 0
Participantes 1