All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.dao.dao4comm.annotation.dialect.mysql.MySqlOneToOneHandler Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.dao.dao4comm.annotation.dialect.mysql;

import com.mycomm.IProtocol.sql.annotation.MyOneToOne;
import com.mycomm.IProtocol.sql.annotation.MyTable;
import com.mycomm.dao.dao4comm.annotation.OneToOneHandler;
import com.mycomm.dao.dao4comm.util.AnnotationParser;
import com.mycomm.dao.dao4comm.util.InstanceBuilder;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author jw362j
 */
public class MySqlOneToOneHandler implements OneToOneHandler {

    private static final Logger log = LoggerFactory.getLogger(MySqlOneToOneHandler.class);

    public void handleOneToOne(final JdbcTemplate jdbcTemplate, final Field field, final Object instance, long mappingId) {

        if (jdbcTemplate == null || field == null || instance == null || mappingId < 0) {
            return;
        }
        MyOneToOne onetoone = field.getAnnotation(MyOneToOne.class);
        log.info("here ,i get one to one mapping ,it was mappedBy:" + onetoone.mappedBy());
        final Class subFieldClassType = field.getType();
        log.info("here ,field type:" + field.getType());

        if (subFieldClassType.isAnnotationPresent(MyTable.class)) {
            MyTable myTable = (MyTable) subFieldClassType.getAnnotation(MyTable.class);
        }
        String subFieldTableName = AnnotationParser.LoadAnnotationStructureStrategy(subFieldClassType).getTableName();
        if ("".equals(subFieldTableName) || subFieldTableName == null) {
            throw new RuntimeException("the table name in SQL is null!");
        }
        final String subFieldTableIdColumName = AnnotationParser.LoadAnnotationStructureStrategy(subFieldClassType).getIdColumName();
        if ("".equals(subFieldTableIdColumName) || subFieldTableIdColumName == null) {
            throw new RuntimeException("the table subFieldTableIdColumName in SQL is null!");
        }
        log.info("here TableName is:" + subFieldTableName + ",TableIdColumName:" + subFieldTableIdColumName);

        String sqlFindOne = "SELECT * FROM " + subFieldTableName + " WHERE " + subFieldTableIdColumName + "=?";

        //final Object subFieldInstance = subFieldClassType.newInstance();
//            Field[] fs = subFieldInstance.getClass().getDeclaredFields();
        jdbcTemplate.query(sqlFindOne,
                new Object[]{mappingId},
                new int[]{java.sql.Types.BIGINT},
                new RowMapper() {

            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    Object subFieldInstance = subFieldClassType.newInstance();
                    Field[] fs = subFieldInstance.getClass().getDeclaredFields();
                    InstanceBuilder.buildInstance( rs, subFieldInstance, fs, subFieldTableIdColumName);
                    field.set(instance, subFieldInstance);
                    return subFieldInstance;
                } catch (InstantiationException ex) {
                    log.error(ex.getMessage());
                } catch (IllegalAccessException ex) {
                    log.error(ex.getMessage());
                }
                return null;

            }
        });

        //let us build all values for subFieldInstance
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy