having a mapping entity class & using a shared EntityManager
(
in "service" implementation
@PersistenceContext
private EntityManager entityManager;
)
1) "insert" query
@Transactional
public void scheduleExport(ScheduledExport export) {
if ( export == null ) return;
entityManager.persist(export);
}
2) annotations for ID generated with a sequence:
mapping entity class annotation
@Entity
@Table(name = "SCHEDULED_EXPORTS")
@SequenceGenerator(sequenceName="SEQ_SCHEDULED_EXPORTS",name="SEQ_SCHEDULED_EXPORTS")
public class ScheduledExport implements Serializable {
...
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_SCHEDULED_EXPORTS")
private Integer id;
3) "delete" query
@Transactional
public void deleteExport(ScheduledExport export) {
if ( export == null ) return;
export = entityManager.merge(export);
entityManager.remove(export);
}
first call merge() and then remove() - otherwise getting java.lang.IllegalArgumentException: Removing a detached instance
4) when access to the data source is needed:
(again in service implementation)
@Autowired
DataSource dataSource;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment