2017-03-28 22 views
0

エンティティ休止更新

@Table(name = "SERVICE") 
public class Service implements Serializable { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = 1L; 

    /** The serviceId. */ 
    @Id 
    @SequenceGenerator(name = "SERVICE_SEQ", sequenceName = "SERVICE_SEQ", allocationSize = 1, initialValue = 1) 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SERVICE_SEQ") 
    @Column(name = "SERVICE_ID", columnDefinition = "NUMBER(10)") 

    /** The description. */ 
    @Column(name = "DESCRIPTION", columnDefinition = "VARCHAR(500)") 
    private String description; 

    @OneToMany(targetEntity = ServiceAddress.class, mappedBy = "service", 
    cascade = CascadeType.ALL, orphanRemoval = true) 
    private Set<ServiceAddress> serviceAddressSet; 

} 

@Entity 
@Table(name = "ADDRESS") 
public class ServiceAddress implements Serializable { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SERVICE_ADDRESS_SEQ") 
    @Column(unique = true, name = "SERVICE_ADDRESS_ID", columnDefinition = "NUMBER(10)") 
    private int serviceAddressId; 

    /** The service address. */ 
    @Column(name = "ADDRESS", columnDefinition = "VARCHAR(200)" ,nullable = false) 
    private String address; 

    @ManyToOne 
    @JoinColumn(name = "SERVICE_ID",nullable = false) 
    private Service service; 

    /** 
    * Gets services. 
    * 
    * @return the services 
    */ 
    public Service getService() { 
     return service; 
    } 

    /** 
    * Sets services. 
    * 
    * @param services 
    *   the services to set 
    */ 
    public void setService(Service service) { 
     this.service = service; 
    } 
} 

これらは私のエンティティクラスの2であり、私は特定のためのADDRESSのテーブルに複数の行を削除したいですHibernateを使用してserviceIdを呼び出します。クエリserviceIdを使用できませんでした。なぜなら、ADDRESSテーブルでは、サービスオブジェクトでマップしました。

答えて

0

session.beginTransaction();

Service service = session.load(Service.class, 114); 

    Criteria c = session.createCriteria(Address.class); 

    c.add(Restrictions.eq("service", service)); 

    List<Address> list = c.list(); 

    for (Address as : list) { 

     session.delete(as); 

    } 

    session.getTransaction().commit(); 

これが役立ちますように!