|
| 1 | +package oracle.jdbc.provider.oson.sample; |
| 2 | +import java.math.BigDecimal; |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | +import java.time.OffsetDateTime; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Properties; |
| 14 | + |
| 15 | +import oracle.jdbc.OracleConnection; |
| 16 | +import oracle.jdbc.OracleType; |
| 17 | +import oracle.jdbc.provider.oson.sample.model.Movie; |
| 18 | +import oracle.jdbc.provider.oson.sample.model.Image; |
| 19 | + |
| 20 | +public class NestedStructurePOJO { |
| 21 | + public static void main(String[] args) throws SQLException { |
| 22 | + Properties properties = new Properties(); |
| 23 | + properties.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, "jackson-json-provider"); |
| 24 | + properties.setProperty(OracleConnection.CONNECTION_PROPERTY_JSON_DEFAULT_GET_OBJECT_TYPE, "oracle.sql.json.OracleJsonValue"); |
| 25 | + try (Connection con = JacksonOsonSampleUtil.createConnectionWithProperties(properties)) { |
| 26 | + Statement stmt = con.createStatement(); |
| 27 | + stmt.execute("drop table if exists movie"); |
| 28 | + stmt.execute("create json collection table movie"); |
| 29 | + |
| 30 | + Movie matrix = new Movie( |
| 31 | + 123, |
| 32 | + "The Matrix", |
| 33 | + "Sci-fi", |
| 34 | + BigDecimal.valueOf(353212323), |
| 35 | + OffsetDateTime.now(), |
| 36 | + Arrays.asList( |
| 37 | + new Image("poster.png", "The Matrix"), |
| 38 | + new Image("showtimes.png", "Matrix Showtimes") |
| 39 | + ) |
| 40 | + ); |
| 41 | + |
| 42 | + Movie shawshank = new Movie( |
| 43 | + 456, |
| 44 | + "The Shawshank Redemption", |
| 45 | + "Drama", |
| 46 | + BigDecimal.valueOf(453212345), |
| 47 | + OffsetDateTime.now(), |
| 48 | + null |
| 49 | + ); |
| 50 | + |
| 51 | + PreparedStatement insert = con.prepareStatement("insert into movie values (:1)"); |
| 52 | + insert.setObject(1, matrix, OracleType.JSON); |
| 53 | + insert.execute(); |
| 54 | + |
| 55 | + insert.setObject(1, shawshank, OracleType.JSON); |
| 56 | + insert.execute(); |
| 57 | + |
| 58 | + ResultSet rs = stmt.executeQuery("select * from movie m where m.data.title = 'The Matrix'"); |
| 59 | + rs.next(); |
| 60 | + Movie result = rs.getObject(1, Movie.class); |
| 61 | + System.out.println("Objects are equal: " + matrix.equals(result)); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments