Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. class User extends Record[User] {
  2. val name = "name".TEXT.NOT_NULL
  3. val admin = "admin".BOOLEAN.NOT_NULL.DEFAULT('false')
  4. }
  5.  
  6. object User extends Table[User] {
  7. def byName(n: String): Seq[User] = criteria.add(this.name LIKE n).list
  8. }
  9.  
  10. // example with foreign keys:
  11. class Account extends Record[Account] {
  12. val accountNumber = "acc_number".BIGINT.NOT_NULL
  13. val user = "user_id".REFERENCES(User).ON_DELETE(CASCADE)
  14. val amount = "amount".NUMERIC(10,2).NOT_NULL
  15. }
  16.  
  17. object Account extends Table[Account]
  18.  
  19. import javax.persistence.Entity;
  20. import javax.persistence.GeneratedValue;
  21. import javax.persistence.Id;
  22.  
  23. @Entity { val name = "Users" }
  24. class User {
  25. @Id
  26. @GeneratedValue
  27. var userid:Long = _
  28.  
  29. var login:String = _
  30.  
  31. var password:String = _
  32.  
  33. var firstName:String = _
  34.  
  35. var lastName:String = _
  36. }
  37.  
  38. /*
  39. Corresponding table:
  40.  
  41. CREATE TABLE `users` (
  42. `id` int(11) NOT NULL auto_increment,
  43. `name` varchar(255) default NULL,
  44. `admin` tinyint(1) default '0',
  45. PRIMARY KEY (`id`)
  46. )
  47.  
  48. */
  49.  
  50. import _root_.javax.persistence._
  51.  
  52. @Entity
  53. @Table{val name="users"}
  54. class User {
  55.  
  56. @Id
  57. @Column{val name="id"}
  58. var id: Long = _
  59.  
  60. @Column{val name="name"}
  61. var name: String = _
  62.  
  63. @Column{val name="admin"}
  64. var isAdmin: Boolean = _
  65.  
  66. override def toString = "UserId: " + id + " isAdmin: " + isAdmin + " Name: " + name
  67.  
  68. }
  69.  
  70. class Product(val name: String, val attributes: Set[Attribute])
  71. class Attribute(val name: String, val value: String)
  72. ...
  73.  
  74. val product = new Product("blue jean", Set(new Attribute("colour", "blue"), new Attribute("size", "medium")))
  75. val inserted = mapperDao.insert(ProductEntity, product)
  76. // the persisted entity has an id property:
  77. println("%d : %s".format(inserted.id,inserted))
  78.  
  79. val o=OrderEntity
  80.  
  81. import Query._
  82. val orders = query(select from o where o.totalAmount >= 20.0 and o.totalAmount <= 30.0)
  83. println(orders) // a list of orders
Add Comment
Please, Sign In to add comment