Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.examples.foreignCollection;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertFalse;
  import static org.junit.Assert.assertSame;
  import static org.junit.Assert.assertTrue;
  
  import java.util.List;
  
Main sample routine to show how to do basic operations with the package.

NOTE: We use asserts in a couple of places to verify the results but if this were actual production code, we would have proper error handling.

 
 public class Main {
 
 	// we are using the in-memory H2 database
 	private final static String DATABASE_URL = "jdbc:h2:mem:account";
 
 	private Dao<AccountIntegeraccountDao;
 	private Dao<OrderIntegerorderDao;
 
 	public static void main(String[] argsthrows Exception {
 		// turn our static method into an instance of Main
 		new Main().doMain(args);
 	}
 
 	private void doMain(String[] argsthrows Exception {
 		JdbcConnectionSource connectionSource = null;
 		try {
 			// create our data source
 			connectionSource = new JdbcConnectionSource();
 			// setup our database and DAOs
 			setupDatabase(connectionSource);
 			// read and write some data
 			..println("\n\nIt seems to have worked\n\n");
 		} finally {
 			// destroy the data source which should close underlying connections
 			if (connectionSource != null) {
 				connectionSource.close();
 			}
 		}
 	}

Setup our database and DAOs
 
 	private void setupDatabase(ConnectionSource connectionSourcethrows Exception {
 
 		 = DaoManager.createDao(connectionSourceAccount.class);
 		 = DaoManager.createDao(connectionSourceOrder.class);
 
 		// if you need to create the table
 		TableUtils.createTable(connectionSourceAccount.class);
 		TableUtils.createTable(connectionSourceOrder.class);
 	}
 
 	private void readWriteData() throws Exception {
 		// create an instance of Account
 		String name = "Buzz Lightyear";
 		Account account = new Account(name);
 
 		// persist the account object to the database
 		.create(account);
 
 		// create an associated Order for the Account
 		// Buzz bought 2 of item #21312 for a price of $12.32
 		int quantity1 = 2;
 		int itemNumber1 = 21312;
 		float price1 = 12.32F;
 		Order order1 = new Order(accountitemNumber1price1quantity1);
 		.create(order1);
 
 		// create another Order for the Account
 		// Buzz also bought 1 of item #785 for a price of $7.98
 		int quantity2 = 1;
 		int itemNumber2 = 785;
 		float price2 = 7.98F;
 		Order order2 = new Order(accountitemNumber2price2quantity2);
 		.create(order2);
 
 		Account accountResult = .queryForId(account.getId());
 		ForeignCollection<Orderorders = accountResult.getOrders();
 
 		// sanity checks
 		CloseableIterator<Orderiterator = orders.closeableIterator();
 		try {
			assertTrue(iterator.hasNext());
			Order order = iterator.next();
			assertEquals(itemNumber1order.getItemNumber());
			assertSame(accountResultorder.getAccount());
			assertTrue(iterator.hasNext());
			order = iterator.next();
			assertEquals(itemNumber2order.getItemNumber());
			assertFalse(iterator.hasNext());
finally {
			// must always close our iterators otherwise connections to the database are held open
			iterator.close();
		}
		// create another Order for the Account
		// Buzz also bought 1 of item #785 for a price of $7.98
		int quantity3 = 50;
		int itemNumber3 = 78315;
		float price3 = 72.98F;
		Order order3 = new Order(accountitemNumber3price3quantity3);
		// now let's add this order via the foreign collection
		orders.add(order3);
		// now there are 3 of them in there
		assertEquals(3, orders.size());
		List<OrderorderList = .queryForAll();
		// and 3 in the database
		assertEquals(3, orderList.size());
	}
New to GrepCode? Check out our FAQ X