"'last_insert_id' 不是可以识别的 函数名"的问题的解决

本文解决了一个在SQLServer2000中使用特定方言时出现的last_insert_id函数不可识别的问题,并详细展示了通过调整数据库方言设置及优化代码逻辑实现问题解决的过程。

昨天好郁闷,碰到了个流氓问题, "'last_insert_id' 不是可以识别的 函数名",改了很多代码,都不行;

我的程序是这样的,有个Student类,对应的ADO是StudentADO,当用户一登陆系统,便向SQL Server2000数据库插入一个用户;

Student类:

package hqq.db;

import org.apache.struts.validator.ValidatorActionForm;

 

/**
 * Student generated by MyEclipse - Hibernate Tools
 */

public class Student extends ValidatorActionForm implements java.io.Serializable {


    // Fields   

     private Integer stdId;
     private String stdName;
     private String stdPwd;
    
 /** default constructor */
    public Student() {
    }

   
    /** full constructor */
    public Student(String stdName, String stdPwd) {
        this.stdName = stdName;
        this.stdPwd = stdPwd;
    }

  
    // Property accessors

    public Integer getStdId() {
        return this.stdId;
    }
   
    public void setStdId(Integer stdId) {
        this.stdId = stdId;
    }

    public String getStdName() {
        return this.stdName;
    }
   
    public void setStdName(String stdName) {
        this.stdName = stdName;
    }

    public String getStdPwd() {
        return this.stdPwd;
    }
   
    public void setStdPwd(String stdPwd) {
        this.stdPwd = stdPwd;
    }
  

StudentADO:

package hqq.db;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Student.
 * @see hqq.db.Student
 * @author MyEclipse - Hibernate Tools
 */
public class StudentDAO extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(StudentDAO.class);

    public static String cheshi = "成功调用StudentDAO";
   
  
    private SessionFactory sessionFactory;
   
 protected void initDao() {
  //do nothing
 }
   
    public void save(Student stu) {//主要调用这个方法
        log.debug("saving Student instance");
        try {
         System.out.println("do save student");
         sessionFactory = this.getSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction tx=session.beginTransaction();
   session.save(stu);
   tx.commit();
   session.close();
            log.debug("save Student successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
   
 public void delete(Student persistentInstance) {
        log.debug("deleting Student instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
   
    public Student findById( java.lang.Integer id) {
        log.debug("getting Student instance with id: " + id);
        try {
            Student instance = (Student) getHibernateTemplate()
                    .get("hqq.db.Student", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
   
   
    public List findByExample(Student instance) {
        log.debug("finding Student instance by example");
        try {
            List results = getSession()
                    .createCriteria("hqq.db.Student")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }   
   
    public List findStudent(Student std) {
        log.debug("finding Student instance by example");
        try {
         System.out.println("do findStudent");
         Session session = getSession();
         System.out.println("connect:"+session.isConnected());
         
         Transaction tx=session.beginTransaction();
         
         Query query = session.createQuery("from Student student where student.stdName=:name and student.stdPwd=:pwd");
            //Query query = session.createQuery("from Student student where student.stdName='"+std.getStdName()+"' and student.stdPwd='"+std.getStdPwd()+"'");
            query.setString("name",std.getStdName());
            query.setString("pwd",std.getStdPwd());
            Iterator it = query.iterate();
      while(it.hasNext()){
       Student stud = (Student)it.next();
       System.out.println(stud.getStdName());
      }
           
            List results = query.list();
           
//            Student stu1 = new Student();
//      stu1.setStdName("zhanghsan1");
//      stu1.setStdPwd("zhangshan1");
//      session.save(stu1);
//      
      tx.commit();
            System.out.println("size:"+results.size());
            session.close();
            return results;
        } catch (RuntimeException re) {
           System.out.println(re.getMessage());
            throw re;
        }
    }   
   
    public Student merge(Student detachedInstance) {
        log.debug("merging Student instance");
        try {
            Student result = (Student) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
   
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

 public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
     return (StudentDAO) ctx.getBean("StudentDAO");
 }
}

 

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
   <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </property>
  <property name="url">
   <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh</value>
  </property>
  <property name="username">
   <value>aa</value>
  </property>
  <property name="password">
   <value>aa</value>
  </property>
 </bean>
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>hqq/db/Course.hbm.xml</value>
    <value>hqq/db/Student.hbm.xml</value>
   </list>
  </property>
 </bean>
 
 <bean id="CourseDAO" class="hqq.db.CourseDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="StudentDAO" class="hqq.db.StudentDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 
 
 <bean name="/login" class="hqq.struts.action.LoginAction">
  <property name="studao">
   <ref bean="StudentDAO" />
  </property>
 </bean>
</beans>

原来我使用的数据库的方言不对,应该是org.hibernate.dialect.SQLServerDialect

 

现在可以运行了:)

不对啊,错误内容 insert overwrite table语句中partition子句赋值格式错误,Hive要求partition字段赋值必须是常量或变量替换后的值,且写法应为partition(pt_d='value'),不能包含表达式或字符串拼接。 错误原因:分区字段pt_d类型为string,且赋值中包含未替换变量'00${pre_1day_hd}',可能导致分区值类型不匹配或分区不存在。 修正建议:确保变量${pre_1day_hd}被替换为合法日期字符串,且分区赋值写法为partition(pt_d='${pre_1day_hd}'),去掉前缀'00',避免类型不匹配。 插入字段数量与目标表字段数量不匹配,缺少分区字段pt_d。 错误原因:目标表cb.dwd_cb_ord_business_drill_hd有分区字段pt_d,insert字段列表未包含该分区字段。 修正建议:insert语句中不应在select字段列表包含分区字段pt_d,partition子句中指定即可,当前写法符合要求,无需修改select字段。 字段不存在错误: owner_user_id字段在ods_crm.tmp_dwd_fxiaoke_cb_new_opportunity_obj_hd表中不存在,SQL中where条件使用了owner_user_idlast_time字段在a表中不存在,但select中使用了a.last_time。 receivables_node字段在d表中不存在,但select中使用了d.receivables_node。 receivables_plan_num字段来源d.Field2__c,d表中field2__c字段不存在。 receivables_plan_status字段来源d.status__c,d表中status__c字段不存在。 修正建议: owner_user_id字段无相似字段,删除where条件中owner_user_id相关过滤。 last_time字段无相似字段,删除select中last_time字段。 receivables_node字段无相似字段,删除select中receivables_node字段。 receivables_plan_num字段应改为d.field2__c改为d.field2__c不存在,考虑是否为d.field2__c拼写错误,元信息无该字段,删除该字段。 receivables_plan_status字段d.status__c不存在,删除该字段。 字段类型不兼容错误: user_id字段目标表为bigint,SQL中c.user_idstring,不兼容。 receivables_plan_status字段目标表为int,SQL中来源字段为string,已删除该字段。 is_end字段目标表为bigint,SQL中来源is_received为string,不兼容。 修正建议: user_id字段cast(c.user_id as bigint)。 is_end字段cast(is_received as bigint)。 Hive不支持collect_set作为窗口函数。 collect_set(owner_user_name) over(partition by business_id)用法错误。 修正建议:改为先group by business_id聚合owner_user_name,再join回主表或使用子查询实现。 nvl函数应替换为coalesce函数。 SQL中多处nvl函数使用,替换为coalesce。 注释掉的t2表导致case表达式中t2.name字段缺失,business_status和status字段来源缺失。 修正建议:注释掉的t2表相关case表达式中t2.name改为null或默认值。 这是第一次代码的报错,也是hive,为什么纠错显示00不能用
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值