Quantcast
Channel: GUJ - Tópicos com a tag hibernate
Viewing all articles
Browse latest Browse all 759

Chaves estrangeiras em java CRUD completo com JSP

$
0
0

@Jsjosue8 escreveu:

Alguém experiente pode me ajudar estou aprendendo a trabalhar com chave estrangeiras em Java com JSP. Preciso de fazer um CRUD completo em uma aplicação, que possuo. Estou trabalhando com Hibernate sem frameworks.

A lógica, que estou tentando desenvolver, é COMPOSIÇÃO entre a entidade projetoInicio e termoAberturaProjeto. Um projeto pode possuir um termos, onde, cada termo pertence a um projeto (@OneToOne). No formulário de cadastro do termo deve ter um SELECT, para listar os projetos.

No formulário listo os projetos, segue alguns trechos do código (Ao submeter o formulário chamo O CONTROLE, que pede ajudar a Classe helpe, que verifica qual ação deve ser executada):

<tr class="field">
							<td><p align="justify"><b>Projeto <font color="red">*</font></b></td>
							<td><p align="justify">
								<select id="projetoinicio" name="projetoinicio">
									<option id="projetoinicio">Selecione</option>
									<%
										List<ProjetoInicio> projetoInicios = (List<ProjetoInicio>) request.getAttribute("projetoInicios");
										for(ProjetoInicio ProjetoInicio: projetoInicios){
											out.println("<option value='" + ProjetoInicio.getCodigo() + "'>"
													                      + ProjetoInicio.getProjeto());
										}
									%>	
								</select><p class="hint">[Informar o projeto.]</p>
							</td>
				</tr>

Trechos do Bean.java

@Entity
@Table(name = "termoabertura")

public class TermoAberturaProjeto {

@OneToOne
@JoinColumn(name="projetoinicio", nullable = false)

private ProjetoInicio projetoinicio = new ProjetoInicio(); 

public ProjetoInicio getProjetoinicio() {
		return projetoinicio;
	}
	public void setProjetoinicio(ProjetoInicio projetoinicio) {
		this.projetoinicio = projetoinicio;
	}

Alguns trechos da Classe TermoAberturaProjeto.java

public class CadastrarTermoAberturaProjeto implements InterfaceCommand {
	
	private InterfaceTermoAberturaProjetoDAO termoAberturaProjetoDAO;	
	public CadastrarTermoAberturaProjeto(InterfaceTermoAberturaProjetoDAO termoAberturaProjetoDAO) {
		super();
		this.termoAberturaProjetoDAO = termoAberturaProjetoDAO;
	}
	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response)	throws Exception{
		
		try {
						
			//  Preencher o Bean com os valores do request
			TermoAberturaProjeto termoAberturaProjeto = new TermoAberturaProjeto();			
						
			termoAberturaProjeto.setConcordo(request.getParameter("concordo"));
Aqui setor todos os atributos, porem, não sei como setar a chave estrangeira, aqui? Seria o código do PROJETO (Entidade projetoInicio)

Após setar adiciono e chamo o DAO!!!

Trecho da classe termoAberturaDAO.java

@Override
	public void salvar(TermoAberturaProjeto termoAberturaProjeto) throws Exception  {
		
		Connection con = Conexao.getConnection();
		PreparedStatement ps=null;
		try {
			
			//Validar teste de duplicação de registro aqui!
			//SELECT * FROM projeto WHERE codigodoprojeto = 'codigodoprojeto';
					
			String sqlInsert = "INSERT INTO termoabertura( termoaberturaprojeto, projeto, codigoprojeto, versao, data, autor, notarevisao, aprovacao, objetivo, justificativa, smart, eap, requisito, marco, parteinteressada, restricao,permissao, risco, orcamento, concordo, projetoinicio) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			ps = con.prepareStatement(sqlInsert, Statement.RETURN_GENERATED_KEYS);
			
			ps.setString(1, termoAberturaProjeto.getTermoaberturaprojeto());
.....set
//Não sei, se está certo à linha abaixo:
ps.setObject(21, termoAberturaProjeto.getProjetoinicio().getCodigo());
						
			ps.executeUpdate();
			
			ResultSet rs = ps.getGeneratedKeys();
	        rs.next();
	        Long idGerado = rs.getLong(1);
	        termoAberturaProjeto.setCodigo(idGerado);
						
			ps.close();

Estou aprendendo com muita dificuldade, por isso peço ajuda de alguem experiente! Preciso de adicionar ao CRUD uma ação com chave estrangeira. Obrigado!

Mensagens: 1

Participantes: 1

Ler tópico completo


Viewing all articles
Browse latest Browse all 759

Trending Articles


FORECLOSURE OF REAL ESTATE MORTGAGE


Girasoles para colorear


tagalog love Quotes – Tiwala Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Patama tagalog quotes – Move On Quotes


Ligaw Quotes – Courting Quotes – Sweet Tagalog Quotes


Hugot Lines 2020 Patama : ML Edition


RE: Mutton Pies (frankie241)


EASY COME, EASY GO


Pokemon para colorear


Sapos para colorear


Maganda Quotes – tagalog Quotes – Inggit Quotes


“Tado” Jimenez Quotes Collections Online


Patama Quotes – Tanga love tagalog quotes


Mga Patama sa mga Nagmamahal the Best Tagalog love quotes for you


5 Tagalog Relationship Rules


Re:Mutton Pies (lleechef)


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>