Hibernate 和 MySQL配合的一个one-to-one 关键字教训

今天犯了一个很白痴的问题,调了一天代码. (哭ing)

Cetificate类的映射

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

< hibernate-mapping >
    
< class  name ="hibernate.Certificate"  table ="certificate"  catalog ="schoolproject"  lazy ="true" >
        
< id  name ="id" >
            
< generator  class ="foreign" >
                
< param  name ="property" > student </ param >
            
</ generator >
        
</ id >
        
< one-to-one 
            
name ="student"  
            class
="hibernate.Student"  
            constrained
="true" >
        
</ one-to-one >
        
< property  name ="describe"  type ="string" >
            
< column  name ="`describe`"  length ="100"  not-null ="true"   />
        
</ property >
    
</ class >
</ hibernate-mapping >

请注意:Certificate中的describe列的column属性name应该绝对不应配置为:describe.

describe在oracle和mysql中都是关键字.(oracle 中我最喜爱的命令之一)

应该在其前后都加上 ` 符号(是数字1左边的那个键)

Student类的映射:

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>

< hibernate-mapping >
    
< class  name ="hibernate.Student"  table ="student"  catalog ="schoolproject"  lazy ="true"   >
           
< id  name ="id"  unsaved-value ="null" >
               
< generator  class ="uuid.hex" ></ generator >
           
</ id >
        
< many-to-one  name ="team"  class ="hibernate.Team"  fetch ="select"  cascade ="none" >
            
< column  name ="team_id"  length ="32" />
        
</ many-to-one >
        
< property  name ="name"  type ="string" >
            
< column  name ="name"  length ="20"   />
        
</ property >
        
< property  name ="cardId"  type ="string" >
            
< column  name ="card_id"  length ="20"  not-null ="true"  unique ="true"   />
        
</ property >
        
< property  name ="age"  type ="integer" >
            
< column  name ="age"   />
        
</ property >
        
< one-to-one 
            
name ="certificate"  
            class
="hibernate.Certificate"  
            fetch
="join"  
            cascade
="all" >
        
</ one-to-one >
    
</ class >
    
< query  name ="queryStudentByAgeAndTeam" >
        
<![CDATA[
            from Student s where s.age>:age and s.team = :team
        
]]>
    
</ query >
</ hibernate-mapping >

测试代码:

Session session  =  HibernateSessionFactory.getSession();
        Transaction transaction 
=  session.beginTransaction();
        
long  start_time  =  System.nanoTime();
        
try {
            Student student 
= new Student();
            student.setCardId(
"22");
            student.setAge(
22);
            student.setName(
"tt");
            
            Certificate certificate 
= new Certificate();
            certificate.setDescribe(
"thisisspark");

            student.setCertificate(certificate);
            certificate.setStudent(student);

            session.save(student);
            
//session.save(certificate);
            transaction.commit();
        }
catch (Exception e) {
            transaction.rollback();
            e.printStackTrace();
        }
finally {
            HibernateSessionFactory.closeSession();
        }

        System.out.println(
" total time: "   +  (System.nanoTime() -  start_time) / 1000 / 1000 );

 通过了,调试.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值