Hibernate:改变JDK7和JDK8之间的unprox对象的流程

时间:2017-12-15 11:26:31

标签: java spring hibernate spring-mvc java-8

在我的项目中,我需要取消对hibernate对象的修改,并使用下面给出的unproxying方法在JDK 1.7.0.45中它工作正常但几周之前我们已将JDK版本更新为1.8.0.121并开始获取以下问题

实体类

public abstract class Employee {

    private Long key;

    private Map<String, Rating> ratingsMap;

    public Employee() {
        super();
    }

    public void setRatingsMap(Map<String, Rating> ratingsMap) {
        this.ratingsMap = ratingsMap;
    }

    public Map<String, Rating> getRatingsMap() {
        return ratingsMap;
    }
}

abstract public class AbstractRating {

 private static final long serialVersionUID = 1L;

    private String identifier;

    private Integer ranking;

    private String name;

    private String description;

}

public class Rating extends AbstractRating {

    private RatingScale ratingScale;
}

Rating.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
        [
            <!ENTITY commonfilters SYSTEM "classpath://common.include.xml">
        ]
    >
<hibernate-mapping package="com.mypackage.model">
<class name="AbstractRating" table="CPD_RATING">
<id name="key" type="long">
            <column name="CPD_RATING_SEQNUM" sql-type="number" />
            <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
                <param name="optimizer">com.mypackage.hibernate.id.enhanced.PooledOptimizerMT</param>
                <param name="increment_size">1000</param>
                <param name="sequence_name">SQ_CPD_RATING</param>
            </generator>
        </id>

        <subclass name="Rating" discriminator-value="TALENT_ASSESSMENT">
            <many-to-one name="ratingScale" class="RatingScale">
                <column name="CPD_RATING_SCALE_SEQNUM" sql-type="number" not-null="true" unique-key="XIE1CPD_RATING"/>
            </many-to-one>
        </subclass>
    </class>
</hibernate-mapping>

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
        [
            <!ENTITY commonfilters SYSTEM "classpath://common.include.xml">
        ]
    >
<hibernate-mapping package="com.mypackage.model">
    <class name="Employee" table="EMPLOYEE" abstract="true">
        <id name="key" type="long">
            <column name="EMP_SEQNUM" sql-type="number"/>
            <generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
                <param name="optimizer">com.workscape.hibernate.id.enhanced.PooledOptimizerMT</param>
                <param name="increment_size">1000</param>
                <param name="sequence_name">SQ_EMPLOYEE</param>
            </generator>
        </id>

        <!-- Map to store the transfered talent assessment values -->    
        <map name="ratingsMap" table="EMP_RATING">
            <key column="EMP_SEQNUM"/>
            <map-key column="SCALE_ID" type="string" length="100"/>
            <many-to-many column="RATING_SEQNUM" class="com.workscape.scale.model.Rating"/>
        </map>
    </class>    
</hibernate-mapping>

取消对象的方法

public abstract class PersistentObject {
    public static <T extends PersistentObject> T unproxy(Object proxy) {
        T unproxied = null;
        Hibernate.initialize(proxy);
        if(proxy instanceof HibernateProxy) {
            unproxied = (T)((HibernateProxy)proxy)
                .getHibernateLazyInitializer().getImplementation();
        } else {
            return (T) proxy;
        }

        return unproxied;
    }   
}

EmployeeRatingUpdaterServiceImpl.java

public class EmployeeRatingUpdaterServiceImpl {

     public void updateRating(Employee employee, String ratingScaleId, Rating rating) {
     employee = PersistentObject.unproxy( employee);

     Map<String, Rating> ratingsMap = employee.getRatingsMap(); 

    if(ratingsMap == null) {
        //This is the first time talent assessment ratings are transfered into comp
        ratingsMap = new HashMap<String, Rating>();
    }

    //add or update the rating
    ratingsMap.put(ratingScaleId, dataSetRating);
    employee.setRatingsMap(ratingsMap);
}   

现在在 updateRating() 方法中,我获取了传递的员工对象的ratingsMap的空值。

public class EmployeeServiceImpl {
    private EmployeeRatingUpdaterService employeeRatingUpdaterService;

    public void updateRating(Employee employee) {
        Map<String, Rating> lookUpRatings = xyzProvider.lookUpRatings();
        for(Map.Entry<String, Rating> entry : lookUpRatings.entrySet()) {
            employeeRatingUpdaterService.updateRating(employeeCompensation, entry.getKey(),
                                                      entry.getValue());
        }
        //NOTE : JDK 1.8 Issue here I am getting ratingsMap of employee object as null it was working fine in JDK1.7
    }   
}
  

现在要解决这个问题,我已将上述方法更新为

public void updateRating(Employee employee) {
        Map<String, Rating> lookUpRatings = xyzProvider.lookUpRatings();
      //NOTE : FIX for the issue 
      employee = PersistentObject.unproxy( employee);
      for(Map.Entry<String, Rating> entry : lookUpRatings.entrySet()) {
            employeeRatingUpdaterService.updateRating(employeeCompensation, entry.getKey(),
                                                      entry.getValue());
        }
    }

我不确定在JDK1.8中解包对象的过程中有什么变化?

Hibernate版本:4.2.6.Final 春季版:4.2.1.RELEASE

对此有任何帮助将不胜感激

0 个答案:

没有答案