In one cart can not harness Horse and quivering doe A.S. Pushkin
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.8.6</version> </dependency>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <compilerId>groovy-eclipse-compiler</compilerId> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.6.0-01</version> </dependency> </dependencies> </plugin>
mvn clean package
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ contactmanager --- [INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files
@Entity @Table(name = "CONTACT_TYPES", uniqueConstraints = [ @UniqueConstraint(columnNames = ["code"]), @UniqueConstraint(columnNames = ["name"]) ]) class ContactType { @Id @GeneratedValue Integer id @Basic @Column(unique = true, nullable = true) String code @Basic @Column(unique = true, nullable = true) String name @Basic @Column(nullable = true) Boolean defaulttype = false @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null }
<hibernate-configuration> <session-factory> <mapping class="net.schastny.contactmanager.domain.Contact" /> <mapping class="com.acme.contactmanager.domain.ContactType" /> </session-factory> </hibernate-configuration>
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH, optional = false) private ContactType contacttype; public ContactType getContacttype() { return contacttype; } public void setContacttype(ContactType contacttype) { this.contacttype = contacttype; }
interface ContactTypeDAO { void addContactType(ContactType contactType) List<ContactType> listContactTypes() void removeContactType(Integer id) ContactType getDefault() }
@Repository class ContactTypeDAOImpl implements ContactTypeDAO { @Autowired private SessionFactory sessionFactory; private Session getCurrentSession() { sessionFactory.getCurrentSession() } @Override @Transactional void addContactType(ContactType contactType) { currentSession.save(contactType) } @Override @Transactional List<ContactType> listContactTypes() { // get- get def result = currentSession.createQuery("from ContactType").list() // , , if(!result){ // List<Map<String, Object>>? ! def types = [ // , // [name:'', code:'family', defaulttype: false], [name:'', code:'job', defaulttype: false], [name:'', code:'stuff', defaulttype: true] ] // types.each { type -> ContactType contactType = new ContactType( // Groovy- , // Map code: type.code, name : type.name, defaulttype : type.defaulttype ) currentSession.save(contactType) // << // result result << contactType } } // return result } @Override @Transactional void removeContactType(Integer id) { ContactType contactType = currentSession.get(ContactType.class, id) if (contactType) { currentSession.delete(contactType) } } @Override @Transactional ContactType getDefault() { currentSession.createCriteria(ContactType.class) .add(Restrictions.eq('defaulttype', true)) .uniqueResult() } }
public interface ContactService { // - ... public List<ContactType> listContactType(); }
@Service public class ContactServiceImpl implements ContactService { // - ... @Autowired private ContactTypeDAO contactTypeDAO; @Override @Transactional public List<ContactType> listContactType() { return contactTypeDAO.listContactTypes(); } }
@RequestMapping("/index") public String listContacts(Map<String, Object> map) { map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); // JSP map.put("contactTypeList", contactService.listContactType()); return "contact"; }
Source: https://habr.com/ru/post/145158/
All Articles