I agree with what Sandro said, for this particular situation of yours. It is interesting that you always analyze the scenario you are developing. For example, in your case it is interesting to make the 1:1 relationship in the same table, because it is little information, but let’s imagine a different scenario:
You are developing for example a vehicle rental application, so you have a Rent table for example:
(Tabela - Aluguel)
id(PK)
idCarro(FK) ManyToOne
idMotorista(FK) ManyToOne
dataDevolucao
dataEntrega
dataPedido
valorTotal
And for every Rent you need an Insurance Policy:
(Tabela - ApoliceSeguro)
id(PK)
protecaoCausasNaturais
protecaoRoubo
protecaoTerceiro
valorFranquia
You noticed how the scenario changed, if you put the 1:1 relationship on the same table, your table will get a lot of information that isn’t hers. The insurance, it belongs to the rent, but it has the insurance information, so it’s a separation of responsibilities.
So for this specific scenario in the Rent table, you will get a FK as follows:
idApoliceSeguro(FK) OneToOne
About the need to perform more than one INSERT. Some framework can assist you in this task, such as Hibernate. Let’s imagine that you try to perform only (1) INSERT. The error will occur saying that the object is referencing an unsaved instance, save first this unsaved instance before downloading the data into the database. The instance he’s referring to is Apoliceseguro.
If you are using Hibernate for example, you could create a DAO from the serguro apolice, save it before, so it becomes a persisted object and managed by Entitymanager and it will have the code to be able to save the Rent.
Or, in the @Onetoone annotation, you use the property (Cascade=Cascadetype.ALL) for all operations or only for certain operations such as PERSIST, MERGE, etc.
But in your specific case, I have a suggestion, separate the responsibilities and use a relationship (Manytomany), I would do as follows:
(TABELA - usuario)
id(PK)
nome
senha
(TABELA - grupo)
id(FK)
descricao
nome
(TABELA - usuario_grupo)
id_usuario(FK)
id_grupo(FK)
That way I can manage as follows:
- GROUP = Administrator, Manager, Employee, ...
- User = user (X) belongs to the Administrator group
- User = user (y) belongs to the Manager group
And so on and so forth...
This way you manage in your application the rights that each group has and which users will receive these rights.
That does not answer the OP question.
– ramaral