Опытный
 
Профиль
Группа: Участник
Сообщений: 802
Регистрация: 8.9.2004
Репутация: нет Всего: 1
|
Привет всем, никак не могу разобраться с этим Hibernate. Объясните пожалуйста. Значит так, есть у меня классы Request и Location, у них отношения ManyToMany, замапил их через посреднический класс(таблицу) RequestLocation то есть как ManyToOne и OneToMany. В итоге имеем Классы следующего вида. Request Код | /** * (Start,[Via1,Via2,..Vian], Ziel) */ package de.hacon.requests;
import java.io.Serializable; import java.util.HashSet; import java.util.List; import java.util.Set;
import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table;
import org.hibernate.annotations.CollectionOfElements; import org.hibernate.annotations.IndexColumn;
/** * @author Unkis * */ @Entity @Table(name = "Request") public class Request implements Serializable {
@Id @Column(name = "request_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@OneToMany(mappedBy = "request") private Set<RequestLocation> locations = new HashSet<RequestLocation>();
@CollectionOfElements @IndexColumn(name = "pc_position", base = 1) private List<String> productChoices;
@Basic @Column(nullable = false) private int language;
@OneToOne(cascade = CascadeType.ALL) private SearchDirection searchDirection;
public Request() { super(); // TODO Auto-generated constructor stub }
public Request(Set<RequestLocation> locations, List<String> productChoices, int language, SearchDirection searchDirection) { super(); this.locations = locations; this.productChoices = productChoices; this.language = language; this.searchDirection = searchDirection; }
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public int getLanguage() { return language; }
public void setLanguage(int language) { this.language = language; }
public Set<RequestLocation> getLocations() { return locations; }
public void setLocations(Set<RequestLocation> locations) { this.locations = locations; }
public List<String> getProductChoices() { return productChoices; }
public void setProductChoices(List<String> productChoices) { this.productChoices = productChoices; }
public SearchDirection getSearchDirection() { return searchDirection; }
public void setSearchDirection(SearchDirection searchDirection) { this.searchDirection = searchDirection; }
}
|
Location Код | package de.hacon.requests;
import java.io.Serializable; import java.util.Collection; import java.util.HashSet; import java.util.Set;
import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table;
@Entity @Table(name = "Location") public class Location implements Serializable {
/** * Id of location */ @Id @Column(name = "location_id") private Long location_id; @OneToMany(mappedBy ="location") private Set<RequestLocation> requestLocations = new HashSet<RequestLocation>(); /** * Type of location (STA - Station, ADR - Address, POI - Point of Interest) */ @Basic @Column(nullable = false, length = 3) private String type;
/** * Name of location(Sity,Street Nr) */ @Basic @Column(nullable = false, length = 100) private String name; /** * X-Coordinate of location */ @Basic @Column(nullable = false) private int xCoordinate; /** * Y-Coordinate of location */ @Basic @Column(nullable = false) private int yCoordinate; public Location() { super(); // TODO Auto-generated constructor stub } public Location(Long location_id, Set<RequestLocation> resuests, String type, String name, int coordinate, int coordinate2) { super(); this.location_id = location_id; this.requestLocations = resuests; this.type = type; this.name = name; xCoordinate = coordinate; yCoordinate = coordinate2; }
public Long getLocation_id() { return location_id; }
public void setLocation_id(Long location_id) { this.location_id = location_id; } public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getType() { return type; }
public void setType(String type) { this.type = type; } public int getXCoordinate() { return xCoordinate; }
public void setXCoordinate(int coordinate) { xCoordinate = coordinate; } public int getYCoordinate() { return yCoordinate; }
public void setYCoordinate(int coordinate) { yCoordinate = coordinate; }
public Set<RequestLocation> getResuests() { return requestLocations; }
public void setResuests(Set<RequestLocation> resuests) { this.requestLocations = resuests; }
}
|
RequestLocation Код | package de.hacon.requests;
import java.io.Serializable;
import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table;
@Entity @Table(name="request_location") public class RequestLocation implements Serializable {
@Embeddable public static class Id implements Serializable{ @Column(name = "location_id") private Long locationId; @Column(name = "request_id") private Long requestId;
public Id() { super(); // TODO Auto-generated constructor stub }
public Id(Long locationId, Long requestId) { super(); this.locationId = locationId; this.requestId = requestId; } public boolean equals(Object o){ if (o != null && o instanceof Id) { Id that =(Id)o; return this.locationId.equals(that.locationId)&&this.requestId.equals(that.requestId); } else { return false; } } public int hashCode(){ return locationId.hashCode()+requestId.hashCode(); } } @EmbeddedId private Id id = new Id(); @ManyToOne() @JoinColumn(name="request_id",insertable = false ,updatable=false) private Request request; @ManyToOne() @JoinColumn(name="location_id",insertable = false, updatable=false) private Location location; @Basic @Column(nullable = false) private int number; /** * Information über den Standortpunkt z.B.(S-Start,V-Via,Z-Ziel) */ @Basic @Column(nullable = false) private char directionType; /** * Method for distributed connection search */ @Basic @Column(nullable = false) private String ringInfoMethod; /** * Optioonally a time offset in minutes can be set(implied access time) */ @Basic @Column(nullable = true) private String timeOffsetMin; public RequestLocation() { super(); // TODO Auto-generated constructor stub }
public RequestLocation(Request request, Location location, int number, char directionType, String ringInfoMethod, String timeOffsetMin) { super(); this.request = request; this.location = location; this.number = number; this.directionType = directionType; this.ringInfoMethod = ringInfoMethod; this.timeOffsetMin = timeOffsetMin; }
public char getDirectionType() { return directionType; }
public void setDirectionType(char directionType) { this.directionType = directionType; }
public Id getId() { return id; } public void setId(Id id) { this.id = id; }
public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getRingInfoMethod() { return ringInfoMethod; } public void setRingInfoMethod(String ringInfoMethod) { this.ringInfoMethod = ringInfoMethod; } public String getTimeOffsetMin() { return timeOffsetMin; } public void setTimeOffsetMin(String timeOffsetMin) { this.timeOffsetMin = timeOffsetMin; } public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; } public Request getRequest() { return request; } public void setRequest(Request request) { this.request = request; } }
|
SearchDirection Код | /** * Algorithmic search direction. */ package de.hacon.requests;
import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;
@Entity public class SearchDirection implements Serializable { private long id; private int flag; private int noConnForw; private int noConnBackw; public SearchDirection() { super(); // TODO Auto-generated constructor stub } public SearchDirection(int flag, int noConnForw, int noConnBackw) { super(); this.flag = flag; this.noConnForw = noConnForw; this.noConnBackw = noConnBackw; } @Column(nullable = false) public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } @Column(nullable = false) public int getNoConnBackw() { return noConnBackw; } public void setNoConnBackw(int noConnBackw) { this.noConnBackw = noConnBackw; } @Column(nullable = false) public int getNoConnForw() { return noConnForw; } public void setNoConnForw(int noConnForw) { this.noConnForw = noConnForw; } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long getId() { return id; } public void setId(long id) { this.id = id; }
}
|
Проблем с мапингом не возникло, всё замапленно правильно и создаются таблицы как надо. Теперь самое интересное как мне сохранить к примеру такое состояния. request1 содержит два location1 и location2.request2 содержит location1 и location3я начинаю так, а как дальше? Код | public void start() { emf = Persistence.createEntityManagerFactory("example", new Properties()); entityManager = emf.createEntityManager(); tx2 = entityManager.getTransaction(); Location l1 = new Location(); l1.setLocation_id(1L); l1.setName("L1"); l1.setType("S"); l1.setXCoordinate(1); l1.setYCoordinate(1); Location l2 = new Location(); l2.setLocation_id(2L); l2.setName("L2"); l2.setType("S"); l2.setXCoordinate(2); l2.setYCoordinate(2); Location l3 = new Location(); l3.setLocation_id(3L); l3.setName("L3"); l3.setType("S"); l3.setXCoordinate(3); l3.setYCoordinate(3); Request r1 = new Request(); r1.setLanguage(1); ArrayList<String> pc = new ArrayList<String>(); pc.add("1"); r1.setProductChoices(pc); r1.setSearchDirection(new SearchDirection(1,1,1)); Request r2 = new Request(); r2.setLanguage(2); ArrayList<String> pc2 = new ArrayList<String>(); pc2.add("2"); r2.setProductChoices(pc2); r2.setSearchDirection(new SearchDirection(2,2,2)); RequestLocation r1_l1 = new RequestLocation(r1,l1,5,'s',"s","s"); RequestLocation r1_l2 = new RequestLocation(r1,l2,5,'s',"s","s"); RequestLocation r2_l1 = new RequestLocation(r2,l1,5,'s',"s","s"); RequestLocation r2_l3 = new RequestLocation(r2,l3,5,'s',"s","s"); tx2.begin(); //и вот здесь как-то надо сохранять но как, что первое сохранять, в какой последовательности? ............................... ............................... ............................... ............................... tx2.commit();
}
|
Ребята подскажите как дальше? Что в какой последовательности сохранять? Ещё интересно что у Request и Location есть атрибут RequestLocation, что в него писать? Вообщем замкнуты круг какой-то получается.
--------------------
www.unkis.com
|