Hibernamte一对一单向外键关联

本文介绍了一个使用 Java 和 Hibernate 实现的学生信息管理系统。该系统包括学生表和身份证信息表两个实体,通过双向关联关系进行连接。文章提供了完整的代码实现,包括实体类定义、配置文件设置以及测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SudentsPk.java ackage com.mao;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator;

//身份证类 @Entity public class StudentsPk { @Id @GeneratedValue(generator="Pk") @GenericGenerator(name="Pk",strategy="assigned") @Column(length=10) private String Pk; private String name;

<!--双向关联关系 @OneToOne(mappedBy="idcard") private Students st; public Students getSt() { return st; } public void setSt(Students st) { this.st = st; } -->

public StudentsPk(){

}

public StudentsPk(String pk, String name) { super(); Pk = pk; this.name = name; }

public String getPk() { return Pk; }

public void setPk(String pk) { Pk = pk; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

}

2 Studnets.java package com.mao;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne;

import org.hibernate.type.TrueFalseType;

@Entity(name="hu_studnets") public class Students {

private int sid;//学号 private String phone; private String post; private StudentsPk idcard; public Students(){

}

public Students(int sid, String phone, String post,StudentsPk idcard) { super(); this.sid = sid; this.phone = phone; this.post = post; this.idcard=idcard; } @Id @GeneratedValue public int getSid() { return sid; }

public void setSid(int sid) { this.sid = sid; }

public String getPhone() { return phone; }

public void setPhone(String phone) { this.phone = phone; }

public String getPost() { return post; }

public void setPost(String post) { this.post = post; }

@OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="Pk",unique=true) public StudentsPk getIdcard() { return idcard; }

public void setIdcard(StudentsPk idcard) { this.idcard = idcard; }

}

3 hibernte.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://127.0.0.1:3306/maodongbang?useUnicode=true&amp;characterEncoding=UTF-8 </property> <property name="connection.username">root</property> <property name="connection.password">mft131415138</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property>

   <property name="show_sql">true</property>
   <property name="format_sql">true</property>
   <property name="hbm2ddl.auto">create</property>
   
  <mapping class="com.mao.Students"/>
  <mapping class="com.mao.StudentsPk"/>
</session-factory>

</hibernate-configuration>

4Test.java package com.mao;

import javax.xml.validation.Schema;

import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test { @org.junit.Test public void test(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExport export=new SchemaExport(config); export.create(true,true); } }

转载于:https://my.oschina.net/u/2511906/blog/841917

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值