How to map a composite key with JPA and Hibernate?

前端 未结 8 1315
旧时难觅i
旧时难觅i 2020-11-22 10:37

In this code, how to generate a Java class for the composite key (how to composite key in hibernate):

create table Time (
     levelStation int(15) not null,         


        
8条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 11:03

    Another option is to map is as a Map of composite elements in the ConfPath table.

    This mapping would benefit from an index on (ConfPathID,levelStation) though.

    public class ConfPath {
        private Map timeForLevelStation = new HashMap();
    
        public Time getTime(long levelStation) {
            return timeForLevelStation.get(levelStation);
        }
    
        public void putTime(long levelStation, Time newValue) {
            timeForLevelStation.put(levelStation, newValue);
        }
    }
    
    public class Time {
        String src;
        String dst;
        long distance;
        long price;
    
        public long getDistance() {
            return distance;
        }
    
        public void setDistance(long distance) {
            this.distance = distance;
        }
    
        public String getDst() {
            return dst;
        }
    
        public void setDst(String dst) {
            this.dst = dst;
        }
    
        public long getPrice() {
            return price;
        }
    
        public void setPrice(long price) {
            this.price = price;
        }
    
        public String getSrc() {
            return src;
        }
    
        public void setSrc(String src) {
            this.src = src;
        }
    }
    

    Mapping:

    
        
            
        
        
            
            
            
                
                
                
                
            
        
    
    

提交回复
热议问题