1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.connector.store.jpa.model.common;
25
26 import javax.persistence.Column;
27 import javax.persistence.Entity;
28 import javax.persistence.EntityManager;
29 import javax.persistence.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.NoResultException;
35 import javax.persistence.Query;
36 import javax.persistence.Table;
37 import org.hibernate.annotations.Index;
38 import org.modeshape.common.util.CheckArg;
39
40
41
42
43
44 @Entity
45 @Table( name = "DNA_NAMESPACES" )
46 @org.hibernate.annotations.Table( appliesTo = "DNA_NAMESPACES", indexes = @Index( name = "NS_URI_INX", columnNames = {"URI"} ) )
47 @NamedQueries( {@NamedQuery( name = "NamespaceEntity.findAll", query = "select ns from NamespaceEntity as ns" ),
48 @NamedQuery( name = "NamespaceEntity.findByUri", query = "select ns from NamespaceEntity as ns where ns.uri = ?1" )} )
49 public class NamespaceEntity {
50
51 @Id
52 @GeneratedValue( strategy = GenerationType.AUTO )
53 private Long id;
54
55 @Column( name = "URI", nullable = true, unique = false, length = 512, updatable = false )
56 private String uri;
57
58
59
60
61 public NamespaceEntity() {
62 }
63
64
65
66
67 public NamespaceEntity( String uri ) {
68 setUri(uri);
69 }
70
71
72
73
74 public Long getId() {
75 return id;
76 }
77
78
79
80
81 public void setId( Long id ) {
82 this.id = id;
83 }
84
85
86
87
88 public String getUri() {
89 return uri;
90 }
91
92
93
94
95 public void setUri( String uri ) {
96 this.uri = uri;
97 }
98
99
100
101
102
103
104 @Override
105 public int hashCode() {
106 return id.hashCode();
107 }
108
109
110
111
112
113
114 @Override
115 public boolean equals( Object obj ) {
116 if (obj == this) return true;
117 if (obj instanceof NamespaceEntity) {
118 NamespaceEntity that = (NamespaceEntity)obj;
119 if (!this.id.equals(that.id)) return false;
120 if (!this.uri.equals(that.uri)) return false;
121 return true;
122 }
123 return false;
124 }
125
126
127
128
129
130
131 @Override
132 public String toString() {
133 return uri;
134 }
135
136
137
138
139
140
141
142
143
144 public static NamespaceEntity findByUri( EntityManager manager,
145 String uri ) {
146 return findByUri(manager, uri, true);
147 }
148
149
150
151
152
153
154
155
156
157
158 public static NamespaceEntity findByUri( EntityManager manager,
159 String uri,
160 boolean createIfRequired ) {
161 CheckArg.isNotNull(manager, "manager");
162 CheckArg.isNotNull(uri, "uri");
163 Query query = manager.createNamedQuery("NamespaceEntity.findByUri");
164 query.setParameter(1, uri);
165 try {
166 return (NamespaceEntity)query.getSingleResult();
167 } catch (NoResultException e) {
168 if (!createIfRequired) return null;
169 NamespaceEntity namespace = new NamespaceEntity(uri);
170 manager.persist(namespace);
171 return namespace;
172 }
173 }
174 }