Ola a todos. Sou novato em Java e Hibernate. To num projeto que caiu no meu colo, por isso nao tive muito tempo para olhar alguma documentação. Vou tentar ser bem sucinto:
1) As tabelas
CREATE TABLE [dbo].[Pai](
[Pai_id] [int] IDENTITY(1,1) NOT NULL,
[Pai_descr] [varchar](20) NULL,
CONSTRAINT [PK_Pai] PRIMARY KEY CLUSTERED
(
[Pai_id] ASC
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Filho](
[Filho_id] [int] IDENTITY(1,1) NOT NULL,
[Pai_Id] [int] NOT NULL,
[Filho_descr] [varchar](20) NULL,
CONSTRAINT [PK_Filho] PRIMARY KEY CLUSTERED
(
[Filho_id] ASC
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Filho] WITH CHECK ADD CONSTRAINT [FK_Filho_Pai] FOREIGN KEY([Pai_Id]) REFERENCES [dbo].[Pai] ([Pai_id])
GO
ALTER TABLE [dbo].[Filho] CHECK CONSTRAINT [FK_Filho_Pai]
GO
O mapeamento no HIbernate (Ainda nao vi como funciona com Annotations)
<hibernate-mapping>
<class name="Pai" table="PAI">
<meta attribute="class-description">
Table Pai
</meta>
<id name="PaiId" type="java.lang.Integer" column="Pai_id">
<generator class="native"/>
</id>
<property name="descr" type="java.lang.String" column="Pai_descr" length="20" />
<set name="filhos" table="filhos" cascade="all" inverse="true">
<key column="Pai_ID" />
<many-to-many column="Filho_ID" class="Filho" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Filho" table="FILHO">
<meta attribute="class-description">
Table Filho
</meta>
<id name="FilhoId" type="java.lang.Integer" column="Filho_id">
<generator class="native"/>
</id>
<property name="descr" type="java.lang.String" column="Filho_descr" length="20" />
</class>
</hibernate-mapping>
public class Teste {
public static void main(String[] args) {
Teste t = new Teste();
t.saveFamilia();
}
public void saveFamilia() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
Integer paiId = null;
try {
transaction = session.beginTransaction();
Pai pai = new Pai();
pai.setDescr("Valmir");
Filho filho = new Filho();
filho.setDescr("Isadora");
pai.getFilhos().add(filho);
paiId = (Integer) session.save(pai);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
Hibernate: insert into PAI (Pai_descr) values (?)
Hibernate: insert into FILHO (Pai_Id, Filho_descr) values (?, ?)
4875 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 515, SQLState: 23000
4875 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot insert the value NULL into column 'Pai_Id', table 'TesteHibernate.dbo.Filho'; column does not allow nulls. INSERT fails.
org.hibernate.exception.ConstraintViolationException: could not insert: [Filho]
Como fica o save() neste caso? O Hibernate não deveria saber que estou inserindo um pai e portanto pegar o Id do Pai e usa-lo para inserir o Filho automaticamente, fazendo todo o insert (do Pai e dos filhos) numa operação só?
Porque devo salvar o Pai, setar o id do Pai na classe Filho e depois fazer um update (como abaixo)?
transaction = session.beginTransaction();
Pai pai = new Pai();
pai.setDescr("Valmir");
paiId = (Integer) session.save(pai);
Filho filho = new Filho();
filho.setDescr("Isadora");
[b]filho.setPaiId(paiId);[/b]
pai.getFilhos().add(filho);
session.update(pai);
transaction.commit();
Obrigado pela ajuda
